这是代码:打印(“欢迎来到我的猜谜游戏,你能得到神奇的数字提示吗,它在 1 到 100 之间”)
import random
Magic_number = random.randrange(1, 100)
print(Magic_number)
guess = int(input("Enter your guess:"))
guess_limit = 5
guess_counter = 1
out_of_guesses = False
print("You have", str(guess_limit - guess_counter), "tries left")
while not out_of_guesses:
guess = int(input("Enter guess: "))
if guess == Magic_number:
print("Well done you got it!!")
exit(0)
elif guess < Magic_number:
print("That number is too small, try again")
elif guess > Magic_number:
print("That number is too high try again")
guess_counter += 1
print("You have", str(guess_limit - guess_counter), "tries left")
# exit clause
if guess_limit == guess_counter:
out_of_guesses = True
print("Game over, sorry")
即使我在第一次尝试时就猜对了,它也会做这样的事情: 欢迎来到我的猜谜游戏 你能得到神奇的数字提示吗,它在 1 到 100 之间 47 输入你的猜测:47 你还剩 4 次尝试 输入猜测:47干得好,你明白了!
正如您所看到的,尽管我在第一次尝试时是正确的,但它没有被计算在内。Ps.(这就是我屏幕底部显示的代码执行位置。)
富国沪深
相关分类