'how do I create variable that is a random character from a list [closed]

list1 = ["yummy", "brown", "cool"] list2 = ["fox", "putin", "pluto"] while True: ch1 = input("Choose the First Character of Your Password, yummy_brown_cool: ") ch2 = input("Choose the Second Character of Your Password, fox_putin_pluto: ") password = ch1 + " " + ch2 break if ch1 not in list1 or ch2 not in list2: print("invalid password")

this is the code. now I wanna create 2 variables (guess1 and guess2) which are from list1 and list2. they then check if guess1 + " " + guess2 == password, and prints what guess1 is and guess2 is. would be so thankful if you could help



Solution 1:[1]

To choose a random string from a list, you can use the function random.choice:

>>> random.choice(["Hello", "World"])
'Hello'

You can do a similar thing for a random character from a string:

>>> "Hello"[random.randint(0, len("Hello")-1)]
'e'
>>> random.choice("Hello")
'l'

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1