1

So I think I understand why the code is not running past the while statement, but I don't know how to get it to recognize the guess part of it and run through it. I've searched through several questions on the looping part, but I can't actually see execution of it.

num_answer = int(input('What should the answer be? '))
guesses = int(input('How many guesses? '))
answer = int(input('Guess a number: '))

while answer != num_answer and guesses != guesses:
    answer = int(input('Guess a number: '))
    if answer < num_answer:
       print('The number is higher than that.')
       guesses += 1
       answer = int(input('Guess a number: '))
    elif answer > num_answer:
       print('The number is lower than that.')
       guesses += 1
       answer = int(input('Guess a number: '))
    elif answer == num_answer:
       print('You win!')
    elif guesses > guesses and answer != num_answer:
       print("You lose; the number was", num_answer, ".")
1
  • 1
    Neither guesses != guesses nor guesses > guesses will ever be true. Commented Oct 9, 2017 at 18:41

1 Answer 1

1

You are trying to compare a variable to itself. "guesses != guesses" is always going to return false so your "while" is never going to be run. You could change you guesses variable into two separate.

num_answer = int(input('What should the answer be? '))
guessesMax = int(input('How many guesses? '))
answer = int(input('Guess a number: '))

while answer != num_answer and guessesMax != guessesCount:
    answer = int(input('Guess a number: '))
    if answer < num_answer:
       print('The number is higher than that.')
       guessesCount += 1
       answer = int(input('Guess a number: '))
    elif answer > num_answer:
       print('The number is lower than that.')
       guessesCount += 1
       answer = int(input('Guess a number: '))
    elif answer == num_answer:
       print('You win!')
    elif guessesCount == guessesMax and answer != num_answer:
       print("You lose; the number was", num_answer, ".")

Also changed your last elif with a "==". But it is useless because when

guessesCount == guessesMax

the while will not be executed ;)

EDIT: You need to add

guessesCount = 0

On top for it to work. Also in your while

answer = int(input('Guess a number: '))

is executing twice, either you put it in the beggining of your loop or in each "if, elif, else" but now in both ;)

Sign up to request clarification or add additional context in comments.

1 Comment

I edited my code to add a different variable to equal guessesMax, but I can't get the while loop to end when guessesCount == guessesMax

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.