Speed calculating algorithm?

Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: May 2009
Posts: 231
Reputation: sravan953 has a little shameless behaviour in the past 
Solved Threads: 28
sravan953's Avatar
sravan953 sravan953 is offline Offline
Posting Whiz in Training

Speed calculating algorithm?

 
0
  #1
Jul 5th, 2009
Hey guys,

I want to make a simple program which calculates speed, distance and time.

This is the code I used:
  1. a=input("1)Find speed\n2)Find distance\n3)Find time\n4)Quit\n")
  2.  
  3. while a!=4:
  4. if a==1:
  5. d=input("\nEnter distance: ")
  6. t=input("Enter time: ")
  7. print ("Speed: "+str(d/t)+"\n")
  8. elif a==2:
  9. s=input("\nEnter speed: ")
  10. t=input("Enter time: ")
  11. print ("Distance :"+str(s*t)+"\n")
  12. elif a==3:
  13. s=input("\nEnter Speed: ")
  14. d=input("Enter distance: ")
  15. print ("Time :"+str(d/s)+"\n")
  16. 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:
  1. s=input("Speed: ")
  2. d=input("Distance: ")
  3. t=input("Time: ")
  4.  
  5. #Considering 1- is the symbol for 'find'
  6.  
  7. s=d/t
  8. if(s==1-):
  9. print (s)
  10. elif(d==1-):
  11. print (d)
  12. elif(t==1-):
  13. 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
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 1,540
Reputation: Lardmeister is an unknown quantity at this point 
Solved Threads: 22
Lardmeister's Avatar
Lardmeister Lardmeister is offline Offline
Posting Virtuoso

Re: Speed calculating algorithm?

 
0
  #2
Jul 5th, 2009
Assuming you are using Python2, you could do it like this:
  1. print("Enter zero for the value you are looking for:")
  2.  
  3. s = d = t = 0
  4.  
  5. while True:
  6.  
  7. s = input("Speed (miles/hour): ")
  8. d = input("Distance (miles): ")
  9. t = input("Time (hours): ")
  10.  
  11. if s == 0:
  12. s = d/float(t)
  13. f = "You have gone %.3f miles in %.3f hours, speed = %.3f"
  14. print f % (d, t, s)
  15. if d == 0:
  16. d = s * float(t)
  17. f = "You have gone at %.3f miles/h for %.3f hours, distance = %.3f"
  18. print f % (s, t, d)
  19. if t == 0:
  20. t = d/float(s)
  21. f = "You have gone %.3f miles at %.3f miles/h, time = %.3f"
  22. print f % (d, s, t)
  23.  
  24. yn = raw_input( "Any more calculations (y or n)? ")
  25. if 'n' in yn.lower():
  26. break
I upped my sanitary measures, up yours!
Reply With Quote Quick reply to this message  
Join Date: Jul 2007
Posts: 489
Reputation: shadwickman will become famous soon enough shadwickman will become famous soon enough 
Solved Threads: 76
shadwickman's Avatar
shadwickman shadwickman is offline Offline
Posting Pro in Training

Re: Speed calculating algorithm?

 
0
  #3
Jul 5th, 2009
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
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
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 1,546
Reputation: Ene Uran has a spectacular aura about Ene Uran has a spectacular aura about 
Solved Threads: 174
Ene Uran's Avatar
Ene Uran Ene Uran is offline Offline
Posting Virtuoso

Re: Speed calculating algorithm?

 
0
  #4
Jul 5th, 2009
@sravan953
also remember that in Python2 ' /' is an integer division by default.
For instance 12/15 gives you a zero. This has changed in Python3.
drink her pretty
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 4,047
Reputation: vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice 
Solved Threads: 935
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
DaniWeb's Hypocrite

Re: Speed calculating algorithm?

 
0
  #5
Jul 8th, 2009
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.
May 'the Google' be with you!
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Python Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC