我是新手,所以请原谅下面的混乱…我正试图写一个数字猜谜游戏。计算机应该随机生成一个介于1到10之间的数字。用户只允许3次尝试正确猜测数字。一个用户要么猜对了,要么尝试完了,我应该让程序问用户,如果他们想再次玩,游戏应该重新启动。下面是我想到的。我想我让这件事变得复杂得多…我做错什么了,因为它不起作用?在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)