What is wrong with the following code? Identity at least 10 things wrong with this code.
(python)
What is wrong with the following code? Identity at least 10 things wrong with this code.
- import random
- func generateSpeed(start, end):
- ""Generate a random speed from start - end""
- return int((end - start + 1) * random.random()) + start
- green == 0
- yellow = 0
- red = 0
- for counter range(20):
- //Generate a random speed from 1 - 80
- speed = generateSpeed(10, 80)
- print("The speed is", speed, "MPH")
- if speed < 0
- continue:
- #if speed >= 50, then classification = Green
- if speed >= 50:
- green + 1
- #if speed < 50 and speed >= 25, then classification = Yellow
- elif speed < 50 and speed >= 25:
- yellow += 1
- #if speed < 25, then classification = Red
- else:
- red += red
- else:
- print("\nTRAFFIC SUMMARY:")
- print("# Green observations (traffic moving faster than 50 m.p.h.):", green)
- print("# Yellow observations (traffic moving between 25 and 50 m.p.h.):", yellow):
- print("# Red observations (traffic moving slower than 25 m.p.h.): " + str(red))
I have corrected the code and explained using comments--
I have provided python code along with code screenshot and also provided output screenshot---------------
Code:
import random
# def keyword used to define the function
def generateSpeed(start, end):
# comment must be starts with '#' rather than ""
# Generate a random speed from start - end
return int((end - start + 1) * random.random()) + start
# to store 0 in variable green we have to used assignment operator(=) ranther than equal operator (==)
green = 0
yellow = 0
red = 0
# in is used
for counter in range(20):
# comment must be starts with '#' rather than //
# Generate a random speed from 1 - 80
speed = generateSpeed(10, 80)
print("The speed is", speed, "MPH")
# every statement end with colon
if speed < 0:
# colon must not used with the statement
continue
#if speed >= 50, then classification = Green
if speed >= 50:
green + 1
#if speed < 50 and speed >= 25, then classification = Yellow
elif speed < 50 and speed >= 25:
yellow += 1
#if speed < 25, then classification = Red
else:
red += red
else:
print("\nTRAFFIC SUMMARY:")
print("# Green observations (traffic moving faster than 50 m.p.h.):", green)
print("# Yellow observations (traffic moving between 25 and 50 m.p.h.):", yellow)
print("# Red observations (traffic moving slower than 25 m.p.h.): " + str(red))
Step by step
Solved in 3 steps with 3 images