Python Projects for Beginners a ten-Week Bootcamp Approach to Python Programming


Step #3: Set Up Try/Except for Mathematical Operation



Download 2,61 Mb.
bet54/200
Sana20.06.2022
Hajmi2,61 Mb.
#681748
1   ...   50   51   52   53   54   55   56   57   ...   200
Bog'liq
Python Projects for Beginners A Ten Week Bootcamp Approach to Python

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.

Download 2,61 Mb.

Do'stlaringiz bilan baham:
1   ...   50   51   52   53   54   55   56   57   ...   200




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©hozir.org 2024
ma'muriyatiga murojaat qiling

kiriting | ro'yxatdan o'tish
    Bosh sahifa
юртда тантана
Боғда битган
Бугун юртда
Эшитганлар жилманглар
Эшитмадим деманглар
битган бодомлар
Yangiariq tumani
qitish marakazi
Raqamli texnologiyalar
ilishida muhokamadan
tasdiqqa tavsiya
tavsiya etilgan
iqtisodiyot kafedrasi
steiermarkischen landesregierung
asarlaringizni yuboring
o'zingizning asarlaringizni
Iltimos faqat
faqat o'zingizning
steierm rkischen
landesregierung fachabteilung
rkischen landesregierung
hamshira loyihasi
loyihasi mavsum
faolyatining oqibatlari
asosiy adabiyotlar
fakulteti ahborot
ahborot havfsizligi
havfsizligi kafedrasi
fanidan bo’yicha
fakulteti iqtisodiyot
boshqaruv fakulteti
chiqarishda boshqaruv
ishlab chiqarishda
iqtisodiyot fakultet
multiservis tarmoqlari
fanidan asosiy
Uzbek fanidan
mavzulari potok
asosidagi multiservis
'aliyyil a'ziym
billahil 'aliyyil
illaa billahil
quvvata illaa
falah' deganida
Kompyuter savodxonligi
bo’yicha mustaqil
'alal falah'
Hayya 'alal
'alas soloh
Hayya 'alas
mavsum boyicha


yuklab olish