Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
error while using the try
#1
When trying to run this portion of code, I received= default 'except:' must be last


try:
range2 = int(input('Enter Low Range:'))
except:
print("You have to enter a number")
range1 = int(input('Enter High Range:'))
except:
print("You have to enter a number")


Please advise
Reply
#2
You used except keyword 2 times, and there is no a single purpose in using the last one. Just delete the last 2 lines of your code
Reply
#3
Good evening, I'm new to programming and my class assignment was to use use exceptions to trap to errors in the code below. but I'm suppose to stop errors and let the program continue to run.

# Program Name: Wk3_Maurice_Brown.py
# Student Name: Maurice Brown
# Instructor: Georgia Brown
# Date: 03/20/2020
# Copy Wrong: This is my work

# This program is a code of Functions for addition, subtraction, multiplication, and division
def Add():
print (var1+var2)
def Subtract():
print (var1-var2)
def Multiply():
print (var1*var2)
def Divide():
print (var1/var2)
try:
range1 = int(input('Enter High Range:'))
except:
print("You have to enter a number")
except:
var1 = int(input('Enter Your First Number: '))
print("You must enter a number")


range1 = int(input('Enter High Range:'))
range2 = int(input('Enter Low Range:'))

var1 = int(input('Enter Your First Number: '))
var2 = int(input('Enter Your Second Number: '))

print('{} + {} = '.format(var1, var2))
print(var1 + var2)

print('{} - {} = '.format(var1, var2))
print(var1 - var2)

print('{} * {} = '.format(var1, var2))
print(var1 * var2)

if int(var1) and int(var2) == 0:
print("Zero is not allowed")
else:
print('{} / {} = '.format(var1, var2))
print(var1 / var2)

# Below is my loop
stop = ''

while stop != 'Yes':
print('Would you like to stop the loop?Type Yes or No')
stop = input()

print('You have stopped the loop')
Reply


Forum Jump:

User Panel Messages

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