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

这是代码:打印(“欢迎来到我的猜谜游戏,你能得到神奇的数字提示吗,它在 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.(这就是我屏幕底部显示的代码执行位置。)


至尊宝的传说
浏览 89回答 1
1回答

富国沪深

if guess == Magic_number您在第 4 行阅读输入后不检查。像这样import randomMagic_number = random.randrange(1, 100)print(Magic_number)guess = int(input("Enter your guess:"))if guess == Magic_number:&nbsp; &nbsp; print ("You won")&nbsp; &nbsp; exit(0)您不需要所有这些变量,检查一下。import randomMagic_number = random.randrange(1, 100)print(Magic_number)guess_limit = 5guess_counter = 0while True:&nbsp; &nbsp; guess = int(input("Enter guess: "))&nbsp; &nbsp; if guess == Magic_number:&nbsp; &nbsp; &nbsp; &nbsp; print("Well done you got it!!")&nbsp; &nbsp; &nbsp; &nbsp; exit(0)&nbsp; &nbsp; elif guess < Magic_number:&nbsp; &nbsp; &nbsp; &nbsp; print("That number is too small, try again")&nbsp; &nbsp; elif guess > Magic_number:&nbsp; &nbsp; &nbsp; &nbsp; print("That number is too high try again")&nbsp; &nbsp; guess_counter += 1&nbsp; &nbsp; print(f"You have {guess_limit - guess_counter} tries left")&nbsp; &nbsp; if guess_counter == guess_limit:&nbsp; &nbsp; &nbsp; &nbsp; print("Game over, sorry")&nbsp; &nbsp; &nbsp; &nbsp; exit(0)
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python