[Python] How to only get random letters and not symbols

Discussão em 'Python' iniciado por Stack, Março 7, 2025.

  1. Stack

    Stack Membro Participativo

    I am trying to make a small Hangman program with unlimited trials. I think I finished building the program, but one problem is when the text includes symbols (like a period (.) for example). I tried multiple ways to only get letters out from a given file, but that didn't work.. Can anyone please help me out??

    My code

    fr = input("What file would you like to open: ")
    f = open(fr, 'r')
    text = f.read()
    x = 0
    textsplit = text.split()
    guess_list = ''



    import random
    rand_letter = random.choice(textsplit)

    for i in range(len(rand_letter)):
    print ('*', end = '')
    print ()
    while x == 0:
    guess = input("Guess a character: ")
    guess_list += guess
    if guess not in rand_letter:
    print("You're wrong bro")
    else:
    for char in rand_letter:
    if char in guess_list:
    print (char, end = ''),

    if char not in guess_list:
    print('*', end = ''),
    if guess_list == rand_letter:
    break



    What I want to solve (example):[​IMG]

    The . in the "father." was actually pretty tough to guess, so I want to remove the symbols and only leave the alphabets.

    Any help/advice/comment would be appreciated!! :)

    Continue reading...

Compartilhe esta Página