| | |
Speed calculating algorithm?
Thread Solved
![]() |
Hey guys,
I want to make a simple program which calculates speed, distance and time.
This is the code I used:
But, my question is, can't it be done like this:
Won't Python itself decide that in if else case 2, since speed and time are provided, it has to calculate time using formula 's=d/t'?
Thanks
I want to make a simple program which calculates speed, distance and time.
This is the code I used:
Python Syntax (Toggle Plain Text)
a=input("1)Find speed\n2)Find distance\n3)Find time\n4)Quit\n") while a!=4: if a==1: d=input("\nEnter distance: ") t=input("Enter time: ") print ("Speed: "+str(d/t)+"\n") elif a==2: s=input("\nEnter speed: ") t=input("Enter time: ") print ("Distance :"+str(s*t)+"\n") elif a==3: s=input("\nEnter Speed: ") d=input("Enter distance: ") print ("Time :"+str(d/s)+"\n") a=input("1)Find speed\n2)Find distance\n3)Find time\n4)Quit\n")
But, my question is, can't it be done like this:
Python Syntax (Toggle Plain Text)
s=input("Speed: ") d=input("Distance: ") t=input("Time: ") #Considering 1- is the symbol for 'find' s=d/t if(s==1-): print (s) elif(d==1-): print (d) elif(t==1-): print (t)
Won't Python itself decide that in if else case 2, since speed and time are provided, it has to calculate time using formula 's=d/t'?
Thanks
Assuming you are using Python2, you could do it like this:
python Syntax (Toggle Plain Text)
print("Enter zero for the value you are looking for:") s = d = t = 0 while True: s = input("Speed (miles/hour): ") d = input("Distance (miles): ") t = input("Time (hours): ") if s == 0: s = d/float(t) f = "You have gone %.3f miles in %.3f hours, speed = %.3f" print f % (d, t, s) if d == 0: d = s * float(t) f = "You have gone at %.3f miles/h for %.3f hours, distance = %.3f" print f % (s, t, d) if t == 0: t = d/float(s) f = "You have gone %.3f miles at %.3f miles/h, time = %.3f" print f % (d, s, t) yn = raw_input( "Any more calculations (y or n)? ") if 'n' in yn.lower(): break
I upped my sanitary measures, up yours!
By putting "s = d / t" before the conditionals would only calculate speed - but what happens when you give speed and distance for example? It wouldn't calculate time. You have to put each individual case within the appropriate conditional branch like Lardmeister did.
P.S. This kind of program is much more useful for something like cosine law, sine law, quadratic formula, etc. as opposed to the simplistic s = d / t which is just a simple, one-step calculation
P.S. This kind of program is much more useful for something like cosine law, sine law, quadratic formula, etc. as opposed to the simplistic s = d / t which is just a simple, one-step calculation
Last edited by shadwickman; Jul 5th, 2009 at 2:00 pm.
"Two good old boys in a fire-apple red convertible. Stoned. Ripped. Twisted. Good people."
- Hunter S. Thompson
my photography
- Hunter S. Thompson
my photography
Something like Lardmeister's code would be a good start of an Equation Solver as set forth in:
http://www.daniweb.com/forums/post159522-7.html
Just make sure you keep your units of measurement correct.
http://www.daniweb.com/forums/post159522-7.html
Just make sure you keep your units of measurement correct.
May 'the Google' be with you!
![]() |
Similar Threads
- Time complexity of algorithm (Computer Science)
- Help me speed up this function? (Python)
- Knight's Tour Speedup. (C)
- memory management in wndows 2000 (Windows NT / 2000 / XP)
- Math in real time (Computer Science)
- 1000% Speed Increase Using Linux Kernel 2.6 (Kernels and Modules)
- Can you guys help me? about Quick Sort Algorithm (C)
- NetZero..."high speed"? (IT Professionals' Lounge)
Other Threads in the Python Forum
- Previous Thread: Help with columns formatting
- Next Thread: wxPython download links broken?
| Thread Tools | Search this Thread |
abrupt accessdenied ansi anti apache application approximation argv array assignment backend beginner binary bluetooth builtin calculator change character converter countpasswordentry curved customdialog dan08 dictionary edit exe file float format function gnu heads homework ideas inches input java keyboard lapse leftmouse library line lines linux list lists loop microphone mouse movingimageswithpygame mysqlquery newb number numbers numeric output parameters parsing path phonebook pointer prime programming py2exe pygame pyopengl python random recursion redirect remote reverse scrolledtext session software sprite sqlite statictext statistics string strings syntax terminal text thread threading time tlapse tuple twoup ubuntu unicode unit urllib urllib2 variable wordgame write wxpython xlib






