For this question, you will write two programs that determine if a car should go
ID: 3839017 • Letter: F
Question
For this question, you will write two programs that determine 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). The following rules should govern the car's decision: "Go" when the light is "green" When the light is "yellow", "Go" if the car can reach the intersection within 5 seconds. Otherwise, the car must "Stop' When the light is "red", "Go" if the car can reach the intersection in 2 seconds Otherwise, the car must "Stop" If the light is any color other than "green", "red", or "yellow", the car must "Stop" Write a program that prints out the proper command (Go" or "Stop") based on the given inputs using only nested if and else statements (i.e., do not use elif, and, or, not, etc.). For example, your program should have a structure similar to: if Boolean_ statement 1: if Boolean_ statement 2 #code block 2a else: #code block 2b else: if boolean statement 3: #code block 3a else: #code block 3b Write a program that prints out the proper command (Go" or "Stop") based on the given inputs using a single if/else statement with compound Boolean expressions. In this case, your program must have a structure like: if compound_ Boolean_ statement: #code block 1 else: #code block 2Explanation / Answer
def car():
color = input('Enter traffic light color: ')
distance = float(input('Enter distance: '))
speed = float(input('Enter distance: '))
time = distance / speed
if color == 'red' or color == 'green' or color == 'yellow':
if color == 'red' or color == 'yellow':
if color == 'red':
if time <= 2:
print 'Go'
else:
print 'Stop'
else:
if time <= 5:
print 'Go'
else:
print 'Stop'
else:
print 'Go'
else:
print 'Go'
def car():
color = input('Enter traffic light color: ')
distance = float(input('Enter distance: '))
speed = float(input('Enter distance: '))
time = distance / speed
if color == 'red' and time < 2:
print 'Go'
else:
print 'Stop'
if color == 'yellow' and time < 5:
print 'Go'
else:
print 'Stop'
if color == 'green':
print 'Go'
else:
print 'Stop'
if color != 'green' or color != 'red' or color != 'yellow':
print 'Go'
else:
print 'Stop'
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.