2

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.

asked Sep 21, 2013 at 20:07
3
  • 1
    input() wasn't for "numbers in general", it was for anything that, when evaluated in the Python interpreter, returns an object. Inserting 1 if True else 2 into an input() prompt returns the integer 1. Commented Sep 21, 2013 at 20:10
  • 2
    It was also a horribly unsafe idea and not what you usually want, which is why they changed it. Commented Sep 21, 2013 at 20:10
  • 2
    Basically, the change is you're supposed to know what input you're expecting, or you should inspect it and determine what to do with it yourself. You can still eval() the string you get from input() yourself, and take proper precautions to prevent it from doing anything funny. Better yet, use ast.literal_eval() - this should be as close to what you need while still being safe. Commented Sep 21, 2013 at 20:11

1 Answer 1

3

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.

answered Sep 21, 2013 at 20:15
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for the help! Sure , there are ways to go around this. I just thought that such a simple program would not require more lines of code.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.