Page 1 of 1

Python program error: Im trying to have the program run under a main function but keep getting speed undefined error it

Posted: Sun Jul 03, 2022 12:01 pm
by answerhappygod
Python program error:
Im trying to have the program run under a main function but keep getting speed undefined error
it works perfect without forcing it into a function but I want it in a function if possible
#Read value of speed from the userdef main(): speed=int(input("What is the speed of vehicle in mph :"))
#Read value of hour from user hours=int(input("How many hours has it travelled? :"))
#Check if the value of speed is postive or negative
if speed<0:
#if speed is negative speed=int(input("Enter positive speed of vehicle in mph :"))
print("Hour Miles travelled")
print("---------------------------")
#Using for loop
for i in range(1,hours+1):
#Print hours and speed
#Distance=speed*time
print(str(i)+" \t "+str(speed*i))
main()
Python Program Error Im Trying To Have The Program Run Under A Main Function But Keep Getting Speed Undefined Error It 1
Python Program Error Im Trying To Have The Program Run Under A Main Function But Keep Getting Speed Undefined Error It 1 (70.46 KiB) Viewed 16 times
RESTART: C:/Users/acroH/Desktop/COSC 1336/Project 5.py How many hours has it travelled? :5 Rectangular Snip Traceback (most recent call last): File "C:/Users/acroH/Desktop/COSC 1336/Project 5.py", line 12, in <module> if speed<0: NameError: name 'speed' is not defined 1336/Project 5.py RESTART: C:/Users/acroH/Desktop/COSC Traceback (most recent call last): File "C:/Users/acroH/Desktop/COSC 1336/Project 5.py", line 12, in <module> if speed<0: NameError: name 'speed' is not defined