我是編程新手,請原諒下面的混亂......我正在嘗試編寫一個猜數字遊戲。計算機應該隨機生成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)
你的問題是什麼? – MattDMo
我希望有人能夠告訴我我做錯了什麼 – BrittLynn
我們不知道你做錯了什麼,因爲你沒有告訴我們任何事情,你所做的一切都是扔掉一堵牆代碼,並期望我們爲您調試它。請閱讀[如何創建最小,完整和可驗證的示例](http://stackoverflow.com/help/mcve)以及如何[提出一個好問題](http://stackoverflow.com/help/how-到問)。您需要包含所需的行爲,包括示例輸入和輸出,實際行爲以及任何錯誤或回溯的**全文**。 *「有人爲我更好」*在這裏不是一個恰當的問題。 – MattDMo