简体   繁体   中英

Python loop behaving strangely

I've been tasked to code a simple guessing game for a school project, but cannot seem to figure out how to get the loops to work correctly. Essentially, the game is letting me replay it as many times as I want (so long as I guess the correct number). If I take too many guesses, it offers me the chance to play again, but no matter how many times I say 'y' or 'n', it just keeps asking me if I want to play again.

At first, I thought this was the issues because the attempts counter wasn't behaving correctly, played around with it and doesn't seem to be the case. I also tried flipping my 'while attempts < 7' and my 'while guess != numbToGuess' loops, but the loop issue only occurs in the attempts loop. I cannot for the life of me figure out where I went wrong. Not sure if it's a case of staring at my code for too long or I'm just failing.

More than happy to provide more details, new to programming, eager to learn (if possible, explain WHY my logic might have been off. I'd rather gain a shift in bad logic that the just get handed the answer).

import sys

play = input("Would you like to play the Guessing Game: y/n ")

attempts = 0
playLoop = 1

if play == 'n':
    playLoop = 0
else:
    while playLoop != 0:
        while playLoop > 0:
            numbToGuess = int(input("Player 1: Please enter a number to be guessed between 1 and 100: "))

            while numbToGuess < 1 or numbToGuess > 100:
                numbToGuess = int(input("I said between 1 and 100, try again. "))

            if numbToGuess > 1 and numbToGuess < 100:
                guess = int(input("Player 2: Make your first guess: "))
                attempts += 1

                while guess != numbToGuess:
                    while attempts < 7:
                        if guess > numbToGuess and guess <= 100:
                            guess = int(input(str(guess) + " is too high, guess again: "))
                            attempts += 1
                        elif guess < numbToGuess and guess >= 1:
                            guess = int(input(str(guess) + " is too low, guess again: "))
                            attempts += 1
                        elif guess > 100:
                            guess = int(input(str(guess) + " is out of range, try guessing below 100: "))
                            attempts += 1
                        elif guess < 1:
                            guess = int(input(str(guess) + " is out of range, try guessing above 0: "))
                            attempts += 1

                    print ("You took too many guesses!")
                    play = input("Would you like to play again?: y/n ")
                    if play == 'y':
                        playLoop += 1
                    else:
                        playLoop == 0

                print ("Congrats! You got it!")
                play = input("Would you like to play again?: y/n ")
                if play == 'y':
                    playLoop += 1
                else:
                    playLoop == 0

sys.exit()

The error is because of two snippets.

  1. After while guess!=numbGuess, you need to check if playLoop is not zero, to continue, else break from loop.
  2. Second, print congrats and following code only if playLoop is not 0

Code:

 import sys
 play = input("Would you like to play the Guessing Game: y/n ")
 attempts = 0
 playLoop = 1

 if play == 'n':
    playLoop = 0
 else:
    while playLoop != 0:
      while playLoop > 0:
        numbToGuess = int(input("Player 1: Please enter a number to be guessed between 1 and 100: "))

        while numbToGuess < 1 or numbToGuess > 100:
            numbToGuess = int(input("I said between 1 and 100, try again. "))

        if numbToGuess > 1 and numbToGuess < 100:
            guess = int(input("Player 2: Make your first guess: "))
            attempts += 1

            while guess != numbToGuess:
                if(playLoop==0):
                    break
                while attempts < 7:
                    if guess > numbToGuess and guess <= 100:
                        guess = int(input(str(guess) + " is too high, guess again: "))
                        attempts += 1
                    elif guess < numbToGuess and guess >= 1:
                        guess = int(input(str(guess) + " is too low, guess again: "))
                        attempts += 1
                    elif guess > 100:
                        guess = int(input(str(guess) + " is out of range, try guessing below 100: "))
                        attempts += 1
                    elif guess < 1:
                        guess = int(input(str(guess) + " is out of range, try guessing above 0: "))
                        attempts += 1

                print ("You took too many guesses!")
                play = input("Would you like to play again?: y/n ")
                if play == 'y':
                    playLoop += 1
                else:
                    playLoop = 0
            if(playLoop!=0):
                print ("Congrats! You got it!")
                play = input("Would you like to play again?: y/n ")
                if play == 'y':
                    playLoop += 1
                else:
                    playLoop == 0

   sys.exit()

@Srinidhi got that right.

the condition check on the while shouldn't duplicate with the answer check condition.

little mistake on checking the playLoop =0,

import sys

play = raw_input("Would you like to play the Guessing Game: y/n ")

attempts = 0
playLoop = 1

if play == 'n':
    playLoop = 0
else:
    while playLoop != 0:
    while playLoop > 0:
        numbToGuess = int(input("Player 1: Please enter a number to be guessed between 1 and 100: "))

        while numbToGuess < 1 or numbToGuess > 100:
            numbToGuess = int(input("I said between 1 and 100, try again. "))

        if numbToGuess > 1 and numbToGuess < 100:
            guess = int(input("Player 2: Make your first guess: "))
            attempts += 1


            while attempts < 7:
                if guess > numbToGuess and guess <= 100:
                    guess = int(input(str(guess) + " is too high, guess again: "))
                    attempts += 1
                elif guess < numbToGuess and guess >= 1:
                    guess = int(input(str(guess) + " is too low, guess again: "))
                    attempts += 1
                elif guess > 100:
                    guess = int(input(str(guess) + " is out of range, try guessing below 100: "))
                    attempts += 1
                elif guess < 1:
                    guess = int(input(str(guess) + " is out of range, try guessing above 0: "))
                    attempts += 1
                else:
                    break;

            if(guess== numbToGuess):
                print ("Congrats! You got it!")
                play = raw_input("Would you like to play again?: y/n ")
                if play == 'y':
                    playLoop += 1
                else:
                    playLoop = 0
            else :
                print ("You took too many guesses!")
                play = raw_input("Would you like to play again?: y/n ")
                if play == 'y':
                    playLoop += 1
                else:
                    playLoop = 0

sys.exit()

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