Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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')

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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')
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading