Can I get the function to pick a random integer/ string value from a list

Hey guys, is there a way I can get the function I am writing to randomly pick an integer or string value from a list?

Below is the example code:

def higher_lower(guess, bet):
  global money
  ace = 1
  List1 = ['ace', 2, 3, 4, 5, 6, 7, 8, 9, 10]
  Value = random.randint(1, 10)

  if guess > Value:
    money = money + bet
    return "Congratulations, you have picked the higher card and won!\n" + "You now have £" + str(money) + " in your account." 

  elif guess < Value:
    money = money - bet
    return "Unfortunately you haven't picked the higher card and lost this time!\n" + "You now have £" + str(money) + " in your account." 

  else:
    money
    return "You have picked a card with the same value as your opponent, it is a tie!\n" + "You now have £" + str(money) + " in your account.

Any help would be greatly appreciated.

Hey there @shaun-dower246642656!! :slightly_smiling_face:

There are a couple ways you can do this. You could use the result of the random.randint() function as an index in a list.

However to make it more simple there is a function, which is also part of the random module, .choice(). It takes a list as an argument, and returns a single item from the list:

import random

lst = ['a', 'b', 1, 2]
print(random.choice(lst))

I recommend reading through the documentation on the random module:

1 Like