在python3中,input
返回一个字符串。您直接将此字符串传递给您的函数,并与从random.choice
. 这是发生的事情:
In [535]: '1' > 2
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-535-6857a5c3feea> in <module>()
----> 1 '1' > 2
TypeError: unorderable types: str() > int()
结论:int
s和str
ings没有可比性。您需要通过调用int
函数来转换输入。
your_guess = int(input(...))
然后,它应该工作:
In [536]: 1 > 2
Out[536]: False
然而,这只能解决一个问题。每次你调用你的函数时,每次都会number
用一个新值重新初始化,这样如果你猜测一个数字并再次调用该函数,你下次将猜测一个完全不同的随机数。您需要在函数numbers
外部进行设置,然后调用该函数,将猜测值和随机数作为参数传递。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句