关键是要猜测从整数区间中选择的随机数,并在固定的尝试次数内进行。
该main
函数询问区间的上限和用户可以给出的猜测次数。然后核心函数应该返回猜测的值,所以当数字正确时,函数应该立即终止。
我在调试时放了一些打印语句,我明白该y
值没有从函数返回到while
语句。core
# -*- coding: utf-8 -*-
def main():
from random import choice
p = input("choose upper limit: ")
t = input("how many attempts: ")
pool = range(p+1)
x = choice(pool)
i = 1
while ((x != y) and (i < t)):
core(x,y)
i += 1
def core(x,y):
y = input("choose a number: ")
if y == x:
print("You gussed the right number!")
return y
elif y > x:
print("The number is lower, try again")
return y
else:
print("The number is higher, try again")
return y