这是我做的随机数猜测程序。它要求用户输入两个数字作为边界,一个高一个低,然后程序将在这两个数字之间选择一个数字。然后,用户必须尝试猜测程序选择的数字。1) 我如何让它询问用户是否愿意再次播放,输入“是”后,程序重新开始,输入“否”后,程序结束?2) 如何创建一个错误陷阱,告诉用户“嘿,你没有输入数字!”并结束程序
def main(): # Main Module
print("Game Over.")
def introduction():
print("Let's play the 'COLD, COLD, HOT!' game.")
print("Here's how it works. You're going to choose two numbers: one small, one big. Once you do that, I'll choose a random number in between those two.")
print("The goal of this game is to guess the number I'm thinking of. If you guess right, then you're HOT ON THE MONEY. If you keep guessing wrong, than you're ICE COLD. Ready? Then let's play!")
small = int(input("Enter your smaller number: "))
large = int(input("Enter your bigger number: "))
print("\n")
return small, large
def game(answer):
c = int(input('Input the number of guesses you want: '))
counter = 1 # Set the value of the counter outside loop.
while counter <= c:
guess = int(input("Input your guess(number) and press the 'Enter' key: "))
if answer > guess:
print("Your guess is too small; you're ICE COLD!")
counter = counter + 1
elif answer < guess:
print("Your guess is too large; you're still ICE COLD!")
counter = counter + 1
elif answer == guess:
print("Your guess is just right; you're HOT ON THE MONEY!")
counter = c + 0.5
if (answer == guess) and (counter < c + 1):
print("You were burning hot this round!")
else:
print("Wow, you were frozen solid this time around.", "The number I \
was thinking of was: " , answer)
def Mystery_Number(a,b):
import random
Mystery_Number = random.randint(a,b) # Random integer from Python
return Mystery_Number # This function returns a random number
A,B = introduction()
number = Mystery_Number(A,B) # Calling Mystery_Number
game(number) # Number is the argument for the game function
main()
如果他们猜对了,你首先必须让游戏返回一些东西:
然后,您将更新“main”函数,使其包含新的“game”函数:
然后,您需要添加一个run_游戏函数来一次运行main多次:
最后,对于错误捕获,您可以执行以下操作:
相关问题 更多 >
编程相关推荐