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()
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
Python program error: Im trying to have the program run under a main function but keep getting speed undefined error it
-
- Site Admin
- Posts: 899603
- Joined: Mon Aug 02, 2021 8:13 am