In: Computer Science
use python
Although clumsy, the if statements and if-else statements can be used to achieve the same effects as if-elif-else statements. Rewrite the speed3 function:
def speed3():
kph=float(input('What is the speed in kph?'))
mph=0.621371*kph
print('The speed is',mph,'mph.')
if mph>80:
print('You are WAY over the speed limit. Your fine is $200!')
elif 65<mph<=80:
print('You are over the speed limit (65mph) . Slow down1').
elif 30<=mph<=65:
print('You are within the speed limit. Good job!')
else:
print('You are too slow. Exit highway and use local roads!')
(a) use only if statement
(b) use only if-else statement
Code:
def speed3():
kph=float(input('What is the speed in kph?'))
mph=0.621371*kph
print('The speed is',mph,'mph.')
if mph>80:
print('You are WAY over the speed limit. Your fine is $200!')
if 65<mph<=80:
print('You are over the speed limit (65mph) . Slow down1')
if 30<=mph<=65:
print('You are within the speed limit. Good job!')
else:
print('You are too slow. Exit highway and use local roads!')
Output: