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

Business, Finance, Economics, Accounting, Operations Management, Computer Science, Electrical Engineering, Mechanical Engineering, Civil Engineering, Chemical Engineering, Algebra, Precalculus, Statistics and Probabilty, Advanced Math, Physics, Chemistry, Biology, Nursing, Psychology, Certifications, Tests, Prep, and more.
Post Reply
answerhappygod
Site Admin
Posts: 899603
Joined: Mon Aug 02, 2021 8:13 am

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

Post 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 14 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
Join a community of subject matter experts. Register for FREE to view solutions, replies, and use search function. Request answer by replying!
Post Reply