有没有办法让我的代码在Python中运行

时间:2020-05-07 14:41:30

标签: python

我正在尝试进行“猜数字”项目。我做了所有“您需要猜测更高/更低”的事情 但是,当我运行代码时,它告诉我猜测更高/更低并完成。它没有给我机会继续或再次猜测 我需要代码来保持运行,我该怎么办呢? (感谢所有回答并回答的人)

import random

random.randint(0, 100)

random = random.randint(0, 100)

guess = float(input("Guess The Number"))

if guess > random + 50:
    print("Too High")
elif guess > random + 25:
    print("A little High")
elif guess > random + 10:
    print("So Close")
elif guess + 50 < random:
    print("Too Low")
elif guess + 25 < random:
    print("A little low")
elif guess + 10 < random:
    print("So Close")
elif guess == random:
    print("Great Job")
else:
    print("Guess Again!")

1 个答案:

答案 0 :(得分:3)

我通常不会回答此类问题,但我会尽量宽大一些。 请搜索有关循环的教程。控制流是编程的基础。不过,这是一个解决方案。请尝试理解代码,而不只是复制粘贴代码。

import random

random = random.randint(0, 100)

guess = float(input("Guess The Number"))

while guess != random:
    if guess > random + 50:
        print("Too High")
    elif guess > random + 25:
        print("A little High")
    elif guess > random + 10:
        print("So Close")
    elif guess + 50 < random:
        print("Too Low")
    elif guess + 25 < random:
        print("A little low")
    elif guess + 10 < random:
        print("So Close")
    else:
        print("Guess Again!")
    guess = float(input("Guess The Number"))

print("Great Job")

说明:

while guess != random

很不言自明。基本上,下面的程序一直运行到猜测=随机为止。一旦guess等于随机,循环便停止。