简体   繁体   中英

python while loop in a while loop ignores the print after winning the game

I'm having an issue with my program. I'm working on a program that lets you play a small game of guessing the correct number. The problem is if you guess the correct number it will not print out: "You guessed it correctly". The program will not continue and will stay stuck on the correct number. This only happens if you have to guess multiple times. I've tried changing the else to a break command but it didn't work. Is there anyone with a suggestion?

This is what I use to test it:

smallest number: 1
biggest number: 10
how many times can u guess: 10

If you try to guess the correct number two or three times (maybe more if u need more guesses) it will not print out you won.

import random

#counts the mistakes
count = 1

#askes to give up a minimum and maximum to guess between
minimum = int(input("what is the smallest number? "))
maximum = int(input("what is the biggest number?  "))

#askes how many times u can guess in total
amount = int(input("How many times can you guess?  "))


#random number between the 2 variables minimum and maximum
x = random.randrange(minimum, maximum)


guess = int(input("guess the number: "))

#while loop until the guess is the same as the random number
while guess != x:

    #this is if u guessed to much u get the error that you've guessed to much
    while count < amount:
            if guess > x:
                print("this is not the correct number, the correct number is lower \n")
                guess = int(input("guess the number: "))
                count += 1

            elif guess < x:
                print("this is not the correct number, the correct number is higher \n")
                guess = int(input("guess the number: "))
                count += 1

    else: print("\n \nYou Lost, You've guessed", x, "times\n")
    break
#this part is not working, only if you guess it at the first time. it should also print this if you guessed it in 3 times
else: print("You guessed it correctly", x)


test = (input("this is just a test if it continues out of the loop "))
print(test)

The main issue is that once guess == x and count < amount you have a while loop running that will never stop, since you don't take new inputs. At that point, you should break out of the loop, which will also conclude the outer loop

This condition is never checked again when the guessed number is correct so the program hangs:

while guess != x:

How about you check for equality as the first condition and break out of the loop if true:

import random
#counts the mistakes
count = 1

#askes to give up a minimum and maximum to guess between
minimum = int(input("what is the smallest number? "))
maximum = int(input("what is the biggest number?  "))

#askes how many times u can guess in total
amount = int(input("How many times can you guess?  "))


#random number between the 2 variables minimum and maximum
x = random.randrange(minimum, maximum)


guess = int(input("guess the number: "))

if guess == x:
    print("You guessed it correctly", x)

else:
    while count < amount:
        if guess > x:
            print("this is not the correct number, the correct number is lower \n")
            guess = int(input("guess the number: "))
            count += 1

        elif guess < x:
            print("this is not the correct number, the correct number is higher \n")
            guess = int(input("guess the number: "))
            count += 1
        else:
            print("You guessed it correctly", x)
            break
    else:
        print("You guessed too many times")

You can do it simply by using one while loop as follows:

import random

#counts the mistakes
count = 1

#askes to give up a minimum and maximum to guess between
minimum = int(input("what is the smallest number? "))
maximum = int(input("what is the biggest number?  "))

#askes how many times u can guess in total
amount = int(input("How many times can you guess?  "))


#random number between the 2 variables minimum and maximum
x = random.randrange(minimum, maximum)


    #this is if u guessed too much u get the error that you've guessed too much
while count <= amount:
  guess = int(input("guess the number: "))

  if guess > x:
      print("this is not the correct number, the correct number is lower \n")
      count += 1

  elif guess < x:
      print("this is not the correct number, the correct number is higher \n")
      count += 1

  else: 
    print("You guessed it correctly", x)
    break

if guess!=x:
  print("\n \nYou Lost, You've guessed", count, "times\n")

As Lukas says, you've kind of created a situation where you get into a loop you can never escape because you don't ask again.

One common pattern you could try is to deliberately make a while loop that will run and run, until you explicitly break out of it (either because the player has guessed too many times, or because they guessed correctly). Also, you can get away with only asking for a guess in one part of your code, inside that while loop, rather than in a few places.

Here's my tweak to your code - one of lots of ways of doing what you want to:

import random

#counts the mistakes
count = 0

#asks to give up a minimum and maximum to guess between
minimum = int(input("what is the smallest number? "))
maximum = int(input("what is the biggest number?  "))

#asks how many times u can guess in total
amount = int(input("How many times can you guess?  "))

#random number between the 2 variables minimum and maximum
x = random.randrange(minimum, maximum)

#while loop until the guess is the same as the random number
while True:

    if count < amount:

        guess = int(input("guess the number: "))

        #this is if u guessed to much u get the error that you've guessed to much
        if guess > x:
            print("this is not the correct number, the correct number is lower \n")
            count += 1

        elif guess < x:
            print("this is not the correct number, the correct number is higher \n")
            count += 1
        else:
            print("You guessed it correctly", x)
            break
    else:
        print("\n \nYou Lost, You've guessed", x, "times\n")

PS: You got pretty close to making it work, so nice one for getting as far as you did!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM