If statements

Can anyone please help me with this as soon as possible.This is a school assignment that I need to do but I am really struggling.

1.Create a python program that determines whether you will get a speeding ticket depending on the type of road you are driving on and the speed you are driving at. The user should input the type of road they are driving on (residential, commercial, or freeway) and the speed they are driving at as an integer. The speed limit on a residential street is 25mph; the speed limit on a commercial street is 35mph; and the speed limit on a freeway is 65mph.

Here’s what I did
input(“Please enter the type of street you are driving on:residential, commercial, freeway”)

input(“Please enter at what speed you are driving at”)

street_residential=input
street_commercial=input
street_freeway=input
speed=input

if street_residential and speed > 25:
print(“You get a speeding ticket”)

if street_commercial and speed > 35:
print(“You get a speeding ticket”)

if street_freeway and speed > 65:
print(“You get a speeding ticket”)

What am I doing wrong?

Here are some other problems I am having issues with. Can you help me?

Branching and Conditionals

  1. Ask the user to input the year he or she was born in, and print the corresponding generation they belong to:
    *GI Generation: 1901-1926
    *Mature/Silents: 1927- 1945
    *Baby Boomers: 1946 and 1964
    *Generation X: 1965 and 1980
    *Generation Y/Millenium: 1981 and 2000
    *Generation Z/Boomlets: Born after 2001

  2. Ask the user to input a number. If the number is divisible by 2 print “even”. If it is divisible by 3 print “divisible by 3”. If it is divisible by 5, print “divisible by 5”. If it’s divisible by 10, print “divisible by 10”. If the number is divisible by more than one of these numbers, print only the highest number it is divisible by (ie. For number = 150, the program should print “divisible by 10”). If the number is not divisible by any number, print "not divisible by 2, 3,
    5 or 10.

3.Allow the user to input two integers, a and b. Use branching to print True if either of the two numbers is equal to 5, or if the sum or difference of the numbers equals 5. Otherwise, print False.

1 Like

Be sure to assign this to a variable, let’s call it zone. Likewise for the next input, as well. Let’s call it speed.

zone = input("...")
speed = input("...")

The ... means fill in the blank with your prompt text.

>>> zone = 'commercial'
>>> speed = 45
>>> if speed > [25, 35, 65][['residential', 'commercial', 'freeway'].index(zone)]:
    print ("You get a speeding ticket!")
else:
    print ("Your speed is within the limit.")

    
You get a speeding ticket!
>>> speed = 35
>>> if speed > [25, 35, 65][['residential', 'commercial', 'freeway'].index(zone)]:
    print ("You get a speeding ticket!")
else:
    print ("Your speed is within the limit.")

    
Your speed is within the limit.
>>> 

Bear in mind that this is not the solution your teacher is looking for. I did that on purpose so you still have to put in the effort to solve it the way expected. It does demonstrate conditional branching, though.

Can you devise a method that only prints 'You get a speeding ticket` in one place within your code? If not, that’s okay, but it will be something you can improve upon when you review this exercise, later.

2 Likes

Thank you for your help!

2 Likes