Use a try-except block to give a special message when bad input is given (so tha
ID: 3821133 • Letter: U
Question
Use a try-except block to give a special message when bad input is given (so that the user knows they put bad input in; assume the user doesn’t have access to the python shell error messages). Bad input includes: letters instead of numbers, missing inputs, or anything that can’t be converted to a float. You also might want to think about which inputs must be positive in order for the calculation to make sense...
Calculating the initial Horizontal Velocity of a projectile. I HAVE THE WORKING CODE HERE BUT I NEED A TRY-EXCEPT BLOCK FOR ERRORS MENTIONED ABOVE IN THE ENTRY OF THE GUI
from tkinter import *
from tkinter import messagebox
root = Tk()
label0 = Label(root, text = "This Calculates the Horizontal distance travelled by a projectile.", bg='black', fg='white')
label = Label(root, text = "Enter the initial values of the projectile here: ", fg='red')
label_1 = Label(root, text = 'Initial Horizontal velocity (m/s):')
entry_1 = Entry(root)
label_2 = Label(root, text = 'Initial Verical velocity (m/s):')
entry_2 = Entry(root)
label_3 = Label(root, text = 'Velocity of wind (m/s):')
entry_3 = Entry(root)
label_4 = Label(root, text = 'Initial Vertical distance (m):')
entry_4 = Entry(root)
label0.grid(row=0)
label.grid(row=1)
label_1.grid(row=2, sticky=E)
label_2.grid(row=3, sticky=E)
entry_1.grid(row=2, column=1)
entry_2.grid(row=3, column=1)
label_3.grid(row=4, sticky=E)
label_4.grid(row=5, sticky=E)
entry_3.grid(row=4, column=1)
entry_4.grid(row=5, column=1)
def value_calculator():
vx = float(entry_1.get())
vy = float(entry_2.get())
w = float(entry_3.get())
d = float(entry_4.get())
v =((((w+vx)**2) + (vy**2))**(1/2))
sin =(vy/v)
cos =((w+vx)/v)
g = 9.80665
#This Breaks down the Distance formula and set each of the equations inside to a variable
A =((v*cos)/g)
B =((v*sin))
C =(2*g*d)
#Calculated the sistance in meters
d =(A*(B+((B**2)+C)**(1/2)))
#Converting meters into yards
y = int((d//.9114))
r1 =(d%.9114) #Takes the remainder for the feet calculation
#Converting to feet
f = int((r1//.3048))
r2 = (r1%.3048) #Takes the remainder for inch calculation
# Converting to inch
i =int((r2//.0254))
if y != 0:
if f != 0:
messagebox.showinfo('Projectile Motion', "The horizontal distance of a projectile is: %i yds %i ft and %i inches" % (y, f, i))
else:
messagebox.showinfo('Projectile Motion', "The horizontal distance of a projectile is: %i yds and %i inches" % (y, i))
elif f != 0:
messagebox.showinfo('Projectile Motion', "The horizontal distance of a projectile is: %i ft and %i inches" % (f, i))
else:
messagebox.showinfo('Projectile Motion', "The horizontal distance of a projectile is: %i inches" % (i))
ComputeButton = Button(root, text='Compute', fg='red', command = value_calculator)
ComputeButton.grid(row=6)
root.mainloop()
Explanation / Answer
from tkinter import *
from tkinter import messagebox
root = Tk()
label0 = Label(root, text = "This Calculates the Horizontal distance travelled by a projectile.", bg='black', fg='white')
label = Label(root, text = "Enter the initial values of the projectile here: ", fg='red')
label_1 = Label(root, text = 'Initial Horizontal velocity (m/s):')
entry_1 = Entry(root)
label_2 = Label(root, text = 'Initial Verical velocity (m/s):')
entry_2 = Entry(root)
label_3 = Label(root, text = 'Velocity of wind (m/s):')
entry_3 = Entry(root)
label_4 = Label(root, text = 'Initial Vertical distance (m):')
entry_4 = Entry(root)
label0.grid(row=0)
label.grid(row=1)
label_1.grid(row=2, sticky=E)
label_2.grid(row=3, sticky=E)
entry_1.grid(row=2, column=1)
entry_2.grid(row=3, column=1)
label_3.grid(row=4, sticky=E)
label_4.grid(row=5, sticky=E)
entry_3.grid(row=4, column=1)
entry_4.grid(row=5, column=1)
def value_calculator():
try:
vx = float(entry_1.get())
vy = float(entry_2.get())
w = float(entry_3.get())
d = float(entry_4.get())
v =((((w+vx)**2) + (vy**2))**(1/2))
sin =(vy/v)
cos =((w+vx)/v)
g = 9.80665
#This Breaks down the Distance formula and set each of the equations inside to a variable
A =((v*cos)/g)
B =((v*sin))
C =(2*g*d)
#Calculated the sistance in meters
d =(A*(B+((B**2)+C)**(1/2)))
#Converting meters into yards
y = int((d//.9114))
r1 =(d%.9114) #Takes the remainder for the feet calculation
#Converting to feet
f = int((r1//.3048))
r2 = (r1%.3048) #Takes the remainder for inch calculation
# Converting to inch
i =int((r2//.0254))
if y != 0:
if f != 0:
messagebox.showinfo('Projectile Motion', "The horizontal distance of a projectile is: %i yds %i ft and %i inches" % (y, f, i))
else:
messagebox.showinfo('Projectile Motion', "The horizontal distance of a projectile is: %i yds and %i inches" % (y, i))
elif f != 0:
messagebox.showinfo('Projectile Motion', "The horizontal distance of a projectile is: %i ft and %i inches" % (f, i))
else:
messagebox.showinfo('Projectile Motion', "The horizontal distance of a projectile is: %i inches" % (i))
except ArithmeticError:
messagebox.showinfo('Error', "The input data resulted in incorrect calculation.")
except ZeroDivisionError:
messagebox.showinfo('Error', "The input data has cause division by zero error.")
except TypeError:
messagebox.showinfo('Error', "The input mismatch, in place of numbers alphabets supplied.")
except ValueError:
messagebox.showinfo('Error', "The input mismatch, in place of numbers alphabets supplied.")
except Exception:
messagebox.showinfo('Error', "Unknown error, please check your input.")
ComputeButton = Button(root, text='Compute', fg='red', command = value_calculator)
ComputeButton.grid(row=6)
root.mainloop()
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.