I am trying to write a program to perform simple arithmetic operations. I want the program to prompt the user for two numbers, and then calculate five results:
- the sum
- the difference
- the product
- the quotient according to both integer
- the floating point division.
Now, I remember that in Python 2, that there was raw_input for strings and input for numbers in general. However, I am just learning Python 3, and input is by default a string, and for numbers I have to specify the type of number I wish to have: i.e. int(input()) or float(input()).
So, for example, let's assume that I want to have exactly this output (using inputs 4 and 2.5):
What is the first number? 4
What is the second number? 2.5
The sum is 6.5
The difference is 1.5
The product is 8.0
The integer quotient is 2
The floating-point quotient is 1.6
I would type this code in Python 2:
x=input ("What is the first number? ")
y=input ("What is the second number? ")
print "The sum is", x+y
print "The difference is", x-y
print "The product is", x*y
print "The integer quotient is", int(x)/int(y)
print "The floating-point quotient is", float(x)/float(y)
However, I can't get it done in Python 3. This is the (wrong) code I am using:
x = int(input("What is the first number? "))
y = int(input("What is the second number? "))
print("The sum is: ", x+y)
print("The difference is: ", x-y)
print("The product is: ", x*y)
print("The integer quotient is: ", x/y)
print("The floating-point quotient is: ", x/y)
Obviously, I get an error message because my second input (y) equals 4.5, which is a float and not an int as defined by my input. I didn't bother putting float(x)/float(y) for the floating-point quotient because that would also be contradictory (thus an error).
I could of course put float instead of int like this:
x = float(input("What is the first number? "))
y = float(input("What is the second number? "))
But in this case, I will get 10.0 for my product (not 10), and my integer quotient is a float (1.6 instead of 2)
I find it really frustrating that in Python 3 I can't ask for a general type number for input (without having to specify if it will be float or int). Therefore, I'm stuck on such simple program, and would greatly appreciate any solution/ explanation.
1 Answer 1
You can try to parse the input as an int, and if that doesn't work, treat it as a float:
def float_or_int(x):
try:
return int(x)
except ValueError:
return float(x)
x = float_or_int(input("What's x?"))
y = float_or_int(input("What's y?"))
To get flooring division in Python 3, you have to explicitly ask for it with the // operator:
print("The integer quotient is:", x//y)
Note that this "integer quotient" operation doesn't really make sense for floating-point input.
input()wasn't for "numbers in general", it was for anything that, when evaluated in the Python interpreter, returns an object. Inserting1 if True else 2into aninput()prompt returns the integer1.eval()the string you get frominput()yourself, and take proper precautions to prevent it from doing anything funny. Better yet, useast.literal_eval()- this should be as close to what you need while still being safe.