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')
letter in userinput is evaluating whether or not the letter is in the user’s input, which we know it is because you’re looping over the letters in
userinput to get
letter. You should remove this part of the if statement, leaving only
if letter in cpu_choice:.
However, the rest of your program looks like it should work, although the user will only have once chance to guess letters because you only get input once.
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')