-1

I have a letter guessing game to complete in Python. The letters the user can pick are "a,b,c, and d." I know how to give them five tries, but when I guess one of the correct letters, I can't break out the loop and congratulate the player.

    g = 0
    n = ("a", "b", "c", "d")

    print("Welcome to the letter game.\nIn order to win you must guess one of     the\ncorrect numbers.")
    l=input('Take a guess: ');
    for g in range(4):

    if l == n:
        break

        else:
            l=input("Wrong. Try again: ")


    if l == n:
            print('Good job, You guessed one of the acceptable letters.')

    if l != n:
            print('Sorry. You could have chosen a, b, c, or d.')

2 Answers2

0

Firstly, you're comparing a letter to a tuple. eg, when you do if l == n, you're saying if 'a' == ("a", "b", "c", "d").

I think what you want here is a while loop.

guesses = 0
while guesses <= 4:
    l = input('Take a guess: ')
    if l in n: # Use 'in' to check if the input is in the tuple
        print('Good job, You guessed one of the acceptable letters.')
        break # Breaks out of the while-loop
    # The code below runs if the input was wrong. An "else" isn't needed.
    print("Wrong. Try again")
    guesses += 1 # Add one guess
    # Goes back to the beginning of the while loop
else: # This runs if the "break" never occured
    print('Sorry. You could have chosen a, b, c, or d.')
TerryA
  • 58,805
  • 11
  • 114
  • 143
0

This keeps most of your code, but rearranges it to meet your goal:

n = ("a", "b", "c", "d")
print('Welcome to the letter game. In order to win')
print('you must guess one of the correct numbers.\n')

guess = input('Take a guess: ');
for _ in range(4):
    if guess in n:
        print('Good job, You guessed one of the acceptable letters.')   
        break      
    guess = input("Wrong. Try again: ")
else:
    print('\nSorry. You could have chosen a, b, c, or d.')

We don't really care about the value of the loop variable, to make this explicit we use '_' in place of the variable.

Levon
  • 138,105
  • 33
  • 200
  • 191