[英]Issues with my number guessing game in python
我是编程新手,所以请原谅下面的混乱……我正在尝试编写数字猜谜游戏。 该计算机应随机生成一个介于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)
对于初学者来说,稍微不那么先进的解决方案:
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!"
我认为您必须检查循环的工作方式,您不能真正真正地手动执行操作,请检查此代码以获取使用循环和函数的示例:
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()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.