Question Details I need my code to make sure that if the user gets the error mes
ID: 3634038 • Letter: Q
Question
Question DetailsI need my code to make sure that if the user gets the error message, to have the program ask them to re-enter valid information., and then prompt the user to enter their speed limit and their speed.... am stuck here am new to Python.
This is what I have:
speedlimit = int(raw_input("What is the speed limit? "))
driverspeed = int(raw_input("What is your speed? "))
if 20 <= speedlimit <= 70:
if driverspeed < speedlimit:
print "You're not speeding."
else:
milesover = driverspeed - speedlimit
print "You're going ", milesover, " mph over the limit."
else:
print "Error: speed limit not in valid range."
Explanation / Answer
Please rate - Thanks
Not sure if you mean to loop only when the last error occurs or when any of the errors you noted occurs. So, here are both scripts for you to choose:
script #1:
# Loop over the speed limit validation only.
#
while True:
speedlimit = int(raw_input("What is the speed limit? "))
if 20 <= speedlimit <= 70:
break
print "Error: speed limit not in valid range."
#
# No more loop here.
#
driverspeed = int(raw_input("What is your speed? "))
if driverspeed >= speedlimit:
milesover = driverspeed - speedlimit
print "You're going ", milesover, " mph over the limit."
else:
print "You're not speeding."
script #2:
# Loop when any one of the noted errors occurs.
#
while True:
speedlimit = int(raw_input("What is the speed limit? "))
driverspeed = int(raw_input("What is your speed? "))
if not( 20 <= speedlimit <= 70 ):
print "Error: speed limit not in valid range."
elif driverspeed >= speedlimit:
milesover = driverspeed - speedlimit
print "You're going ", milesover, " mph over the limit."
else:
print "You're not speeding."
break
Let me know if you have any question - I'll rush over a fix.
/update - Minor simplification to the script #1. No change on functionality.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.