Step #3: Set Up Try/Except for Mathematical Operation
The third, and final step, is to try performing the operation. The reason for setting up a try/except block here is because we must convert the user’s input to floating data types.
We must assume that they may not enter the proper input. Let’s see how this cell will work:
# step 3: setup try/except for mathematical operation try:
# step 3a: immediately try to convert numbers input to floats num1, num2 = float(num1), float(num2) # step 3b: perform operation and print result if operation == "add":
result = num1 + num2
print( "{ } + { } = { }".format(num1, num2, result) ) elif operation == "subtract": result = num1 - num2 print( "{ } - { } = { }".format(num1, num2, result) ) elif operation == "multiply": result = num1 * num2 print( "{ } * { } = { }".format(num1, num2, result) ) elif operation == "divide": result = num1 / num2 print( "{ } / { } = { }".format(num1, num2, result) ) else:
# else will be hit if they didn't chose an option correctly print("Sorry, but '{ }' is not an option.".format(operation) ) except:
# steb 3c: print error print("Error: Improper numbers used. Please try again.")
Go ahead and run that cell. There’s a lot going on here so let’s start from the top. We set up a try block and immediately convert the user’s input to floats. If this causes an error, the except clause will be hit and output that an error occurred rather than the program breaking. If the input can be converted, then we set up an if/elif/else statement to perform the calculation and output the proper result. If they didn’t input a proper operation, then we let them know. This cell is dependent on the previous two. If you’re getting errors, rerun the previous cells.
Final Output
Now that we’ve created the logic for our program in three separate cells, we can now put it all together in one. Let’s remove all the testing print statements. You can essentially take all the code from the three cells and paste them into one cell, resulting in the following:
# step 1: ask user for calculation to be performed operation = input("Would you like to add/subtract/multiply/divide? ").
lower( )
# step 2: ask for numbers, alert order matters for subtracting and dividing if operation == "subtract" or operation == "divide": print( "You chose { }.".format(operation) ) print("Please keep in mind that the order of your numbers matter.") num1 = input("What is the first number? ") num2 = input("What is the second number? ") # step 3: setup try/except for mathematical operation try:
# step 3a: immediately try to convert numbers input to floats num1, num2 = float(num1), float(num2) # step 3b: perform operation and print result if operation == "add":
result = num1 + num2 print( "{ } + { } = { }".format(num1, num2, result) ) elif operation == "subtract": result = num1 - num2 print( "{ } - { } = { }".format(num1, num2, result) ) elif operation == "multiply": result = num1 * num2 print( "{ } * { } = { }".format(num1, num2, result) ) elif operation == "divide": result = num1 / num2 print( "{ } / { } = { }".format(num1, num2, result) ) else:
# else will be hit if they didn't chose an option correctly print("Sorry, but '{ }' is not an option.".format(operation) ) except:
# steb 3c: print error
print("Error: Improper numbers used. Please try again.")
Go ahead and run that cell. Now you’re able to run a single cell to get our program to work from start to finish. It’s not perfect, but it gives you the ability to perform simple calculations. As always, try to break the program, change a line around, and make it your own.
Congratulations on finishing another project! As simple as this calculator may be, we have shown the ability to use logic, take user input and convert it, and check for errors.
Do'stlaringiz bilan baham: |