我已经在Python 2.7.14中编写了一个猜数字游戏,遇到了一些奇怪的事情:
我要求输入整数并进行检查,但是尽管不接受其他字符串或字符,但始终接受变量的名称(此处为“ a”)。当用户即使不允许使用变量名也可以输入变量名时,这不是问题吗?
我的代码是:
from random import randint
a = randint(0,10)
b = input("enter a number between 0 and 10 ")
print "you entered :", b
if type(b) != int:
print "please enter only integers"
else:
if b < a:
print "b is smaller than a"
elif b == a:
print "b is equal to a"
else:
print "b is NOT smaller than a"
print "number was", a