Why can't the code below receive the input value as an integer? From what I looked up, it is correct to use raw_input()
but from Python 3.x, raw_input()
has changed to input()
.
play = True
while play:
x = input("Enter a number: ")
y = input("Enter a number: ")
print(x + y)
print(x - y)
print(x * y)
print(x / y)
print(x % y)
if input("Play again? ") == "no":
play = False
Python 2.x
In Python 2.x, there were two functions that received user input values. One is input
and the other is raw_input
. The difference between the two is that raw_input
accepts the input as just a string of its state, and input
evaluates the input and returns the result. For example,
>>> import sys
>>> sys.version
'2.7.6 (default, Mar 22 2014, 22:59:56) \n[GCC 4.8.2]'
>>> data = input("Enter a number: ")
Enter a number: 5 + 17
>>> data, type(data)
(22, <type 'int'>)
You can see that the input value 5+17
is evaluated and returned to 22
. If 5+17
is evaluated here, it detects that you want to add two numbers and decides the result as int
type. Therefore, the result of input
becomes 22
and is stored in the variable data
. input
is the same as raw_input
used with val
function.
>>> data = eval(raw_input("Enter a number: "))
Enter a number: 5 + 17
>>> data, type(data)
(22, <type 'int'>)
Python : Note 2. in x input we need to be careful when using the
. The reason is is to the detailed answer.
In any case, raw_input
returns the input value to the entered string itself without evaluating it separately.
>>> import sys
>>> sys.version
'2.7.6 (default, Mar 22 2014, 22:59:56) \n[GCC 4.8.2]'
>>> data = raw_input("Enter a number: ")
Enter a number: 5 + 17
>>> data, type(data)
('5 + 17', <type 'str'>)
Python 3.x
>>> import sys
>>> sys.version
'3.4.0 (default, Apr 11 2014, 13:05:11) \n[GCC 4.8.2]'
>>> data = input("Enter a number: ")
Enter a number: 5 + 17
>>> data, type(data)
('5 + 17', <class 'str'>)
Solution
To answer the question, Python 3.x does not change the data type by evaluating the input value separately, so you should change the type directly to In addition, the is the input, and recognize and which functions are converted to decimal to. If The entered value is invalid Apart from the question, I think we can change the program you wrote to this way. Remove the variable int
using the int function. p>
x = int(input("Enter a number: "))
y = int(input("Enter a number: "))
int
function allows any progressive input and converts it into a decimal number.>>> data = int(input("Enter a number: "), 8)
Enter a number: 777
>>> data
511
>>> data = int(input("Enter a number: "), 16)
Enter a number: FFFF
>>> data
65535
>>> data = int(input("Enter a number: "), 2)
Enter a number: 10101010101
>>> data
1365
valueerror to the
.>>> data = int(input("Enter a number: "), 2)
Enter a number: 1234
Traceback (most recent call last):
File "<input>", line 1, in <module>
ValueError: invalid literal for int() with base 2: '1234'
while True:
...
...
if input("Play again? ") == "no":
break
play
and use break
and while True
.
© 2025 OneMinuteCode. All rights reserved.