Question

Problem 4 For this question, you will write a program called drive.py that determines if a...

Problem 4

For this question, you will write a program called drive.py that determines if a car should go through an intersection or stop. Your program will prompt the user for the colour of the traffic light at the intersection (a string), the distance to the intersection in meters (a float), and the speed of the car in meters per second (a float). Your program must then determine if the car should go through the intersection or stop.

The following rules should govern the car’s decision:

1. “Go” when the light is “green”

2. When the light is “yellow”, “Go” if the car will reach the intersection within 5 seconds. Otherwise, the car must “Stop”

3. When the light is “red”, “Go” if the car will reach the intersection in 2 seconds. Otherwise, the car must “Stop”

4. If the light is any color other than “green”, “red”, or “yellow”, the car must “Stop”

Hint: you can use the programming example done is class as the starting point.

use python

0 0
Add a comment Improve this question Transcribed image text
Answer #1

If you have any doubts please comment below. Please give upvote if you like this.

Python Code : drive.py

# Reading input from user
colour = input("Colour of the traffic light at the intersection: ")
dist = float(input("The distance to the intersection in meters: "))
speed = float(input("The speed of the car in meters per second: "))

# Calculate time using dist and speed
# Time = distance/speed
time = dist/speed
# Check the condition of colours
if colour == "green": # It satisfies condition print below statement
print("Car should go through an intersection")
elif colour == "yellow":
if time <= 5: # It satisfies condition print below statement
print("Car should go through an intersection")
else:
print("The car must stop")
elif colour == "red":
if time <= 2:# It satisfies condition print below statement
print("Car should go through an intersection")
else:
print("The car must stop")
# condition for If colour other than “green”, “red”, or “yellow”
else:
print("The car must stop")

- drive.py - C:\Users\Vinu Bujji\Desktop\drive.py (3.7.3) File Edit Format Run Options Window Help # Reading input from userPython 3.7.3 Shell х File Edit Shell Debug Options Window Help Python 3.7.3 (v3.7.3:ef4ec6ed12, Mar 25 2019, 21:26:53) [MSC v

Add a comment
Know the answer?
Add Answer to:
Problem 4 For this question, you will write a program called drive.py that determines if a...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • A car is travelling north at a speed 12.9 m/s toward an intersection when the traffic...

    A car is travelling north at a speed 12.9 m/s toward an intersection when the traffic light turns red. It takes the car 4.2 seconds to stop. (a) What is magnitude of the car’s average acceleration? (b) What is the direction of the car’s average acceleration? * east * south * north * west

  • You are driving towards a traffic signal when it turns yellow. You are travelling at 15.5...

    You are driving towards a traffic signal when it turns yellow. You are travelling at 15.5 m/s and as you break, you decelerate at a constant rate of 3.5 m/s2. Your reaction time before breaking is 0.75 s. If the distance to the intersection is 40 m, willl your car stop before the intersection? If the duration of the yellow light was 3.2 seconds, would you have been able to pass the traffic signal before the light turned red?

  • Write and implement a program that controls a traffic light in accordance with the following design...

    Write and implement a program that controls a traffic light in accordance with the following design features/specifications: A)The green light should be on for 10 seconds B)The red light should be on for 5 seconds C)The amber light should be on for 2 seconds D)The push button should cause the light to go to red 1 second after it was depressed.

  • Write and implement a program that controls a traffic light in accordance with the following design...

    Write and implement a program that controls a traffic light in accordance with the following design features/specifications: A)The green light should be on for 10 seconds B)The red light should be on for 5 seconds C)The amber light should be on for 2 seconds D)The push button should cause the light to go to red 1 second after it was depressed.

  • Two cars start from rest at a red stop light. When the light turns green, both...

    Two cars start from rest at a red stop light. When the light turns green, both cars accelerate forward. The blue car accelerates uniformly at a rate of 5.6 m/s2 for 3 seconds. It then continues at a constant speed for 7.8 seconds, before applying the brakes such that the car’s speed decreases uniformly coming to rest 178.16 meters from where it started. The yellow car accelerates uniformly for the entire distance, finally catching the blue car just as the...

  • The city is trying to figure out how long the traffic light should stay yellow at...

    The city is trying to figure out how long the traffic light should stay yellow at an intersection. The speed limit on the road is 45.0 km/h and the intersection is 23.0 m wide. A car is traveling at the speed limit in the positive direction and can brake with an acceleration of ?5.20 m/s2. (a) If the car is to stop on the white line, before entering the intersection, what is the minimum distance from the line at which...

  • Is there anyone out there could help me, please.. Objective: - Write a ladder logic program to s...

    Is there anyone out there could help me, please.. Objective: - Write a ladder logic program to simulate an industrial process. - Explore additional PLC/HMI data types. Program Requirements: Part 1: ~~Red Button will be used to stop the system, Red light only should be on. ~~Green button press: -Yellow Light will flash for 10 seconds. -After elapsed time yellow light goes off, illuminate green light then the conveyor should start. Make sure the buttons remain in proper state. (Not...

  • 2. Consider the following problem associated with synchronizing traffic lights. A particular traffic light (Light A)...

    2. Consider the following problem associated with synchronizing traffic lights. A particular traffic light (Light A) has a cycle as follows: Red = 1 min Green = 1.5 min Yellow = 0.5 min Some distance before Light A is another light (Light B). Due to varying drivers and conditions, the travel time between the two lights varies from vehicle to vehicle. Data suggest that 40% of all cars leaving Light B at a time when Light A is red are...

  • Word Problem) Two two-way streets meet at an intersection controlled by a four-way traffic light. In...

    Word Problem) Two two-way streets meet at an intersection controlled by a four-way traffic light. In the east and west directions, the lights cycle from green to yellow to red. The south-facing lights do the same thing, except that they are red when the east-west lights are green or yellow, and vice versa. However, the north-facing lights are augmented with a green left turn arrow. They cycle red-green arrow-yellow arrow-green-yellow-red. Consider the following additional problem specifications: When the green or...

  • A person driving her car at 49.0 km/hr approaches an intersection just as the traffic light...

    A person driving her car at 49.0 km/hr approaches an intersection just as the traffic light turns yellow. She knows that the yellow light lasts only 1.80 s before turning to red, and she is a distance D = 35.0 m away from the near side of the intersection. If she decides to stop, how far from the intersection will she come to a stop? The intersection is W =11.0 m wide. Her car's maximum deceleration is -5.70 m/s2. Ignore...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT