random.randint在给定范围内不起作用

时间:2019-06-17 13:41:16

标签: python random

我在python中做作业。 我需要编写一个程序来猜测用户在0-100范围内选择的数字。

我试图随机编写程序, 程序将打印随机整数并接收输入(如果其为高或低)。 如果它的值很低,则程序将在它打印的数字和100之间生成一个数字(或者,如果该数字比该代码可以生成的最大数字高)。  但是由于某些原因,当我输入打印的数字很高时,它并没有将更大的数字设置为最大范围。

import random
high = 100
low = 0
tries = 0
print("select a number between 0-100")
x = random.randint(low, high)
print("is it: " +str(x) +"?")
while True:
    y = input("enter high if guess is higher, enter low if guess is lower and enter correct if guess is correct\n")
    if y == "low":
        low = x
        x = random.randint(low, high)
        print("is it: " +str(x) +"?")
        tries +=1
        continue
    elif y == "high":
        hige = x
        x = random.randint(low, high)
        print("is it: " +str(x) +"?")
        tries += 1
        continue
    elif y == "correct":
        print("it took: " +str(tries) +" tries")
        c = input("enter c to close and co to continue")
        if c == "c":
            break
        elif c =="co":
            continue
    else:
        print("enter valid input:\n")
        continue

我希望最大范围是最后生成的代码的最大数字。 谢谢您的帮助:)

2 个答案:

答案 0 :(得分:1)

elif y == "high":
    hige = x

最可能的原因是您打错了写作,而不是写高

答案 1 :(得分:0)

好像您在第17行将变量“ high”拼写为“ hige”一样。