2015-11-07 44 views
1

我是編程新手,請原諒下面的混亂......我正在嘗試編寫一個猜數字遊戲。計算機應該隨機生成1到10之間的數字。用戶只允許3次嘗試正確猜測號碼。一個用戶要麼猜對了,要麼嘗試用完,我應該讓程序詢問用戶他們是否想再次玩,並且遊戲應該重新開始。以下是我想到的。我認爲我使得這件事情變得複雜得多......我做錯了什麼,因爲它不起作用?python中我的猜數字遊戲的問題

import random 


number = random.randint(1,10) 


print "The computer will generate a random number between 1 and 10. Try to guess the number!" 

guess = int(raw_input("Guess a number: ")) 
attempts = 0 


while guess != number and attempts < 4: 
    if guess >= 1 and guess <= 10: 
     print "Sorry, you are wrong." 
    else: 
     print "That is not an integer between 1 and 10 (inclusive)." 
     guess = int(raw_input("Guess another number: ")) 
     attempts = attempts + 1 
     if attempts > 4: 
      print "You've guessed incorrectly and are out of tries..." 
      playAgain = raw_input("Would you like to play again? ") 
      if playAgain = "Yes" or playAgain == "y": 
      import random 
      number = random.randint(1,10) 
      attempts = 0 
      guess = int(raw_input("Guess a number: ")) 
      while guess != number and attempts < 4: 
       if guess >= 1 and guess <= 10: 
        print "Sorry, you are wrong." 
       else: 
        print "That is not an interger between 1 and 10 (inclusive)." 
        guess = int(raw_input("Guess another number: ")) 
        attempts = attempts + 1 
while guess == number:       
    print "Congratulations, you guessed correctly!" 
    playAgain = raw_input("Would you like to play again? ") 
     if playAgain = "Yes" or playAgain == "y": 
      import random 
      number = random.randint(1,10) 
      attempts = 0 
      guess = int(raw_input("Guess a number: ")) 
      while guess != number and attempts < 4: 
       if guess >= 1 and guess <= 10: 
        print "Sorry, you are wrong." 
       else: 
        print "That is not an interger between 1 and 10 (inclusive)." 
        guess = int(raw_input("Guess another number: ")) 
        attempts = attempts + 1 
        if attempts > 3: 
         print "You've guessed incorrectly and are out of tries..." 
         playAgain = raw_input("Would you like to play again? ") 
          if playAgain == "yes" or playAgain == "Yes": 
          import random 
          number = random.randint(1,10) 
          attempts = 0 
          guess = int(raw_input("Guess a number: ")) 
          while guess != number and attempts < 4: 
            if guess >= 1 and guess <= 10: 
            print "Sorry, you are wrong." 
            else: 
             print "That is not an interger between 1 and 10 (inclusive)." 
             guess = int(raw_input("Guess another number: ")) 
             attempts = attempts + 1 
             if attempts > 3: 
              print "You've guessed incorrectly and are out of tries..." 
              playAgain = raw_input("Would you like to play again? ") 
              if playAgain == "yes" or playAgain == "Yes": 
              import random 
              number = random.randint(1,10)   
+0

你的問題是什麼? – MattDMo

+0

我希望有人能夠告訴我我做錯了什麼 – BrittLynn

+0

我們不知道你做錯了什麼,因爲你沒有告訴我們任何事情,你所做的一切都是扔掉一堵牆代碼,並期望我們爲您調試它。請閱讀[如何創建最小,完整和可驗證的示例](http://stackoverflow.com/help/mcve)以及如何[提出一個好問題](http://stackoverflow.com/help/how-到問)。您需要包含所需的行爲,包括示例輸入和輸出,實際行爲以及任何錯誤或回溯的**全文**。 *「有人爲我更好」*在這裏不是一個恰當的問題。 – MattDMo

回答

0

我認爲你必須檢查迴路是如何工作的,你真的不能做手工的東西,檢查這個代碼與循環和功能實現的例子:

import random 

def guessGame(): 
    guessnum = random.randint(1,10) 
    print "Try to guess the number between 1 and 10 in 3 ansers or less" 
    i = 1 
    while i < 4: 
     try: 
      num = int(raw_input("Attempt " + str(i) + ": ")) 
      if num == guessnum: 
       return True 
      else: 
       i+=1 
       print "Try again" 
     except Exception as e: 
      print "Please input a valid number" 
      continue 

def mainLoop(): 
    while True: 
     guessGame() 
     exitopt = raw_input("Good game, wanna try again? y - yes, n - no: ") 
     if exitopt == "y": 
      continue 
     elif exitopt == "n": 
      print "Bye!!!" 
      break 

if __name__ == "__main__": 
    mainLoop() 
1

一點的人不太先進的解決方案誰是初學者:

import random 

attempts = 0 

number = random.randint(1,10)  

while attempts < 4: 
    attempts += 1 
    print number  #print number to help with testing 
    guess = int(raw_input("Guess a number from 1 to 10: ")) 
    if guess == number: 
     print "you guessed the number!", 
     again = raw_input("do you want to play again? y or n ")  
     if again == "y": 
      number = random.randint(1,10) 
      attempts = 0 # gives 4 attempts to a new game 
     else: 
      print "good bye"     
      break  
    elif attempts == 4: 
     print "The game is over!" 
+0

嘗試打印你寫的代碼的某些部分,它有助於理解... –

+0

我不認爲這是一個有效的解決方案...您提示爲每次重播輸入一個數字兩次,併爲每次嘗試生成一個新的隨機數。 –

+0

@ cricket_007詢問用戶是否想再次玩遊戲開始一個新的遊戲,所以你想產生一個新的隨機數,新的遊戲新號碼! –