Python Forum
Help with try, except, else finally
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Help with try, except, else finally
#1
Hi everyone,

Nice to meet you all! I'm building a calculator with python and I want to add try except else and finally. I have added the try except and the calculator is working but it doesn't return the error message if user input is not an integer.

Can you please take a look?
#User inputs math type
operation = input('''
Please type in the math operation you would like to complete:
+ for addition
* for multiplication
''')
#User inputs number
number_1 = int(input('Enter your first number: ')) 

number_2 = int(input('Enter your second number: '))

#If user chooses addition
try: 
    if operation == '+':
        print('{} + {} = '.format(number_1, number_2))
        print(number_1 + number_2)
except Exception:
    print: ("You have not input a number")

#if user chooses multiplication
try:    
    if operation == '*':
        print('{} * {} = '.format(number_1, number_2))
        print(number_1 * number_2)
except: 
    print: ("You have not input a number")
snippsat write Mar-21-2021, 04:29 PM:
Added code tag in your post,look at BBCode on how to use.
Reply


Messages In This Thread
Help with try, except, else finally - by Pytho13 - Mar-21-2021, 04:17 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Am I a retard - else and finally blocks in a try statement RubenF85 6 2,763 Jan-12-2021, 05:56 PM
Last Post: bowlofred
  finally clause Skaperen 6 4,052 Jun-02-2019, 09:02 PM
Last Post: snippsat
  try, except, finally ? microphone_head 3 2,954 Apr-28-2019, 09:36 PM
Last Post: microphone_head

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020