Skip to content Skip to sidebar Skip to footer

"not Equal" != Into Python 3 Not Working

I'm trying to switch a code from Python 2.7.10 to Python 3, and some things aren't working. I just recently was introduced to Python. choice = 2 while choice != 1 and choice != 0:

Solution 1:

The input function returns user input as a string. You can convert it to a number using the int and float methods:

choice = 2while choice != 1and choice != 0:
    choice = input("Hello, no.")
    choice = int(choice)
    if choice != 1and choice != 0:
       print("Not a good code.")

Solution 2:

input in Python 3 is equivalent to Python 2's raw_input; it reads a string, it doesn't try to eval it. This is because eval of user input is intrinsically unsafe.

If you want to do this safely in Python 3, you can wrap the call to input in int to convert to int, or in ast.literal_eval to interpret arbitrary Python literals without opening yourself up to arbitrary code execution.

import ast

choice = 2while choice != 1and choice != 0:
    choice = ast.literal_eval(input("Hello, no."))
    if choice != 1and choice != 0:
       print("Not a good code.")

Post a Comment for ""not Equal" != Into Python 3 Not Working"