为了账号安全,请及时绑定邮箱和手机立即绑定

乍一看,即使我做对了,也没有任何注册

乍一看,即使我做对了,也没有任何注册

至尊宝的传说 2022-12-20 09:47:57
这是代码:打印(“欢迎来到我的猜谜游戏,你能得到神奇的数字提示吗,它在 1 到 100 之间”)import randomMagic_number = random.randrange(1, 100)print(Magic_number)guess = int(input("Enter your guess:"))guess_limit = 5guess_counter = 1out_of_guesses = Falseprint("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 = Trueprint("Game over, sorry")即使我在第一次尝试时就猜对了,它也会做这样的事情: 欢迎来到我的猜谜游戏 你能得到神奇的数字提示吗,它在 1 到 100 之间 47 输入你的猜测:47 你还剩 4 次尝试 输入猜测:47干得好,你明白了!正如您所看到的,尽管我在第一次尝试时是正确的,但它没有被计算在内。Ps.(这就是我屏幕底部显示的代码执行位置。)
查看完整描述

1 回答

?
富国沪深

TA贡献1790条经验 获得超9个赞

if guess == Magic_number您在第 4 行阅读输入后不检查。像这样


import random

Magic_number = random.randrange(1, 100)

print(Magic_number)

guess = int(input("Enter your guess:"))


if guess == Magic_number:

    print ("You won")

    exit(0)

您不需要所有这些变量,检查一下。


import random

Magic_number = random.randrange(1, 100)

print(Magic_number)


guess_limit = 5

guess_counter = 0


while True:

    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(f"You have {guess_limit - guess_counter} tries left")


    if guess_counter == guess_limit:

        print("Game over, sorry")

        exit(0)


查看完整回答
反对 回复 2022-12-20
  • 1 回答
  • 0 关注
  • 88 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信