0

I've written a number guessing game in Python 2.7.14 and encountered something odd:

I ask for an integer as input and check for that, but the name of the variable (here "a") is always accepted, although no other strings or characters are accepted. Isn't this an issue when the user can enter variable names even though they are not allowed?

My code is:

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
tevemadar
  • 12,389
  • 3
  • 21
  • 49
oystermanu
  • 45
  • 1
  • 6
  • If the user enters `"a"`, then `b == "a"`, which is a string, not an int. – Joel Jul 31 '18 at 13:32
  • 3
    It's because `a` is evaluated as the variable `a` you already defined. You should use `raw_input` in Python 2 (or update to Python 3) – Chris_Rands Jul 31 '18 at 13:33
  • 1
    @Chris_Rands That makes sense! I didn't know that input() does an eval() on the input. I will use raw_input() in the future. – oystermanu Aug 02 '18 at 08:08

0 Answers0