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,140 points
15 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
• 56,160 points

Related Questions In Python

0 votes
0 answers
+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,420 points
1,147 views
+3 votes
5 answers

How to read multiple data files in python

Firstly we will import pandas to read ...READ MORE

answered Apr 6, 2018 in Python by DeepCoder786
• 1,700 points
1,038 views
+4 votes
6 answers

Use of "continue" in python

The break statement is used to "break" ...READ MORE

answered Jul 16, 2018 in Python by Omkar
• 67,120 points
118 views
+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 6 in Python by Neha
• 330 points

edited Jul 8 by Kalgi 128 views
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 in Python by Shashank
• 1,350 points
40 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
• 56,160 points
498 views