To receive input from Python as an integer

Asked 2 years ago, Updated 2 years ago, 149 views

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

int python python-2.7 python3

2022-09-22 21:36

1 Answers

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.

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

input in Python 3.x is similar to raw_input in Python 2.x, and raw_input does not exist in Python 3.x.

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 int using the int function.

x = int(input("Enter a number: "))
y = int(input("Enter a number: "))

In addition, the 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
Let me know the base of a number that is the second parameter

is the input, and recognize and which functions are converted to decimal to. If The entered value is invalid 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'

Apart from the question, I think we can change the program you wrote to this way.

while True:
    ...
    ...
    if input("Play again? ") == "no":
        break

Remove the variable play and use break and while True.


2022-09-22 21:36

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.