I’m trying to check if user input is one of the letters in the chosen word by the CPU. Let me know if this is not possible the way I’m trying to do it, thanks.
import random test_list = [ 'yes', 'no'] # guessing list print("Original list is : " + str(test_list)) cpu_choice = cpu_choice=("Random element is :", random.sample(test_list, 1)) print(cpu_choice) # i know it gives the answer i'm just using this to test and get the program to work userinput = input('guess a letter ') for letter in userinput: if letter in userinput == letter in cpu_choice: print('correct') elif print: print('wrong')
I modified it to loop through the cpu_choice instead of the userinput (userinput is just one letter).
The printing of the result is moved out of the loop so the program won’t print ‘wrong’ for every letter in the word that doesn’t match.
userinput = input('guess a letter: ') match = False for letter in cpu_choice: if letter == userinput: match = True break if match: print('correct') else: print('wrong')