Home > Blockchain >  Problem I am facing using the len command and while loop
Problem I am facing using the len command and while loop

Time:06-17

So I have this block of code, which is supposed to figure out how many letters there are in the input. If it is greater than 1 or less than 1(no input), there is an error. However, when running this code, it still prints out "You entered an invalid..." even when I only input in a single letter, which shouldn't be passing through the while loop because its length is only 1. Idk why this is happening any beginner friendly help is appreciated!

letter_guess = input("Enter a single letter to guess: ")
length = len(letter_guess)
while length > 1 or length < 1:
    letter_guess = input("You entered an invalid amount of letters, please guess again: ")

CodePudding user response:

You should add length = len(letter_guess) in while loop after the input. As it's not updating currently.

CodePudding user response:

You just need to update the length variable again when you ask for the next guess.

letter_guess = input("Enter a single letter to guess: ")
length = len(letter_guess)
while length > 1 or length < 1:
    letter_guess = input("You entered an invalid amount of letters, please guess again: ")
    length = len(letter_guess)
  • Related