Read input of integers in Python

0 votes

This code doesn't take Integer input. Whatever I found on the web says to use raw_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

Oct 26, 2018 in Python by findingbugs
• 3,260 points
866 views

1 answer to this question.

0 votes

In the upper version of 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.

As the default inputs are String 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: "))

Also, Python does not need/use semicolons to end lines. So, having them doesn't do anything positive.

answered Oct 26, 2018 by Priyaj
• 58,020 points

Related Questions In Python

0 votes
0 answers
0 votes
1 answer
+2 votes
3 answers

what is the practical use of polymorphism in Python?

Polymorphism is the ability to present the ...READ MORE

answered Mar 31, 2018 in Python by anto.trigg4
• 3,440 points
4,939 views
0 votes
2 answers
+1 vote
2 answers

how can i count the items in a list?

Syntax :            list. count(value) Code: colors = ['red', 'green', ...READ MORE

answered Jul 7, 2019 in Python by Neha
• 330 points

edited Jul 8, 2019 by Kalgi 4,467 views
0 votes
1 answer
0 votes
2 answers

What are the types of dictionary in python?

There are 4 types of dictionary Empty Integer Mixed Dictionary with ...READ MORE

answered Feb 14, 2019 in Python by Shashank
• 1,370 points
1,043 views
0 votes
1 answer

Behaviour of increment and decrement operators in Python

When you want to increment or decrement, ...READ MORE

answered Jul 26, 2018 in Python by Priyaj
• 58,020 points
3,784 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP