2017-10-15 205 views
0

我一直被授权为一个学校项目编写一个简单的猜谜游戏,但似乎无法弄清楚如何让循环正常工作。从本质上讲,游戏让我可以随心所欲地重播它(只要我猜测的是正确的数字)。如果我猜得太多,它给了我再次参加比赛的机会,但无论我说'y'还是'n'多少次,它都会一直问我是否想再次参赛。Python循环行为奇怪

起初,我认为这是问题,因为尝试计数器表现不正确,玩弄它似乎并非如此。我也尝试翻转我的'试图< 7'和我的'while guess!= 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() 

回答

0

错误是由于两个片段。

  1. 经过猜测!= numbGuess之后,您需要检查playLoop是否不为零,继续,否则从循环中断开。
  2. 其次,打印恭喜和下面的代码只有playLoop不为0

代码:

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

@Srinidhi有这种权利。

条件检查时不应该与答案检查条件重复。

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