2017-10-15 209 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()