is user input a letter in the CPU's choice?

Question:

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')
Asked By: jones007

||

Answers:

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.

Answered By: Dash

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: ')[0]
match = False
for letter in cpu_choice[1][0]:
  if letter == userinput:
    match = True
    break
if match:
  print('correct')
else:
  print('wrong')
Answered By: Metahuman Flash
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.