Python循环表现奇怪

时间:2017-10-15 05:53:42

标签: python python-3.x while-loop nested-loops game-loop

我的任务是为一个学校项目编写一个简单的猜谜游戏,但似乎无法弄清楚如何让循环正常工作。从本质上讲,游戏让我多次重播它(只要我想正确的数字)。如果我做了太多的猜测,它会让我有机会再次参加比赛,但无论我多少次说“你好”。或者' n'它只是一直在问我是否想再玩一次。

起初,我认为这是问题所在,因为尝试计数器没有正确行事,玩弄它并且似乎并非如此。我也试过翻转我的'尝试< 7'和我的猜测!= numbToGuess'循环,但循环问题只发生在尝试循环中。我不能为我的生活弄清楚我哪里出错了。不确定是不是长时间盯着我的代码,或者我只是失败了。

非常乐意提供更多详细信息,新编程,渴望学习(如果可能的话,解释为什么我的逻辑可能已经关闭。我更倾向于改变错误的逻辑,以便得到答案)。

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()

2 个答案:

答案 0 :(得分:0)

错误是因为两个片段。

  1. 在猜测!= numbGuess之后,您需要检查playLoop是否为零,继续,否则从循环中断。
  2. 其次,仅当playLoop不为0
  3. 时才打印恭喜和关注代码

    代码:

     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()
    

答案 1 :(得分:0)

@Srinidhi做对了。

while上的条件检查不应与答案检查条件重复。

检查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()