0

Is there a way to figure out characters in a string? This is just an example of a very simple game that I quickly made, but I can't figure out how to tell the computer to read the string for letters. Is there a certain function or such that will do this? Basically, I'm trying to create a way to figure out if the user input matches the correct character of the random string.

import random

word = ['Friend', 'Foe', 'Enchilada', 'Bamboozled', 'Smuggled', 'Flabbergasted']
YOOO = random.choice(word)
UndCrs = ' _ '  * len(YOOO)

print(UndCrs)

while True:

    guess = input('Guess a random word in the alphabet')
    if guess not in 'abcdefghijklmnopqrstuvwxyz':
        print('Only characters please')
    
    # Rest of character string code
    ...
   
    break

My code currently is somewhat correct. With a bit of tweaking, I can fix the rest, just need help on this. Sorry for no formatting. It's late.

Czaporka
  • 2,190
  • 3
  • 10
  • 23
  • 1
    Does this answer your question? [In Python, how to check if a string only contains certain characters?](https://stackoverflow.com/questions/1323364/in-python-how-to-check-if-a-string-only-contains-certain-characters) – Piotr Siupa Dec 12 '22 at 09:21
  • There are only four words in the alphabet and they're "ef", "hi", "no" and "op" – DarkKnight Dec 12 '22 at 09:42

1 Answers1

2

You can do something like:

if not guess.isalpha():
    print("Only letters are allowed!")
David Meu
  • 1,527
  • 9
  • 14