【发布时间】:2015-11-08 07:01:45
【问题描述】:
我是编程新手,所以请原谅下面的混乱......我正在尝试编写一个猜数字游戏。计算机应该随机生成一个介于 1 到 10 之间的数字(包括 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)
【问题讨论】:
-
你有什么问题?
-
我希望有人能告诉我我做错了什么
-
我们不知道您做错了什么,因为您没有告诉我们任何事情,您所做的只是丢掉了一堵代码,并希望我们为您调试它。请阅读How to create a Minimal, Complete, and Verifiable Example 以及如何阅读ask a good question。您需要包含所需的行为,包括示例输入和输出、实际行为以及任何错误或回溯的全文。 “有人为我做得更好”在这里不是一个合适的问题。
-
对于初学者来说,你会随机导入一大堆时间,每次你回答“是”时都将尝试重置为 0。
标签: python python-2.7