Signup/Sign In
Ask Question
Not satisfied by the Answer? Still looking for a better solution?

How can I read inputs as numbers?

Why are x and y strings rather than ints within the below code?

(Note: in Python 2.x use raw_input(). In Python 3.x use input(). raw_input() was renamed to input() in Python 3.x)

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
by

2 Answers

akshay1995
In Python 3.x, raw_input was renamed to input and the Python 2.x input was removed.

This means that, just like raw_input, input in Python 3.x always returns a string object.

To fix the problem, you need to explicitly make those inputs into integers by putting them in int:

x = int(input("Enter a number: "))
y = int(input("Enter a number: "))
sandhya6gczb
For multiple integers in a single line, map might be better.

arr = map(int, input().split())

If the number is already known, (like 2 integers), you can use

num1, num2 = map(int, input().split())

Login / Signup to Answer the Question.