943,640 Members | Top Members by Rank

Ad:
  • Python Discussion Thread
  • Marked Solved
  • Views: 869
  • Python RSS
Jul 5th, 2009
0

Speed calculating algorithm?

Expand Post »
Hey guys,

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

This is the code I used:
Python Syntax (Toggle Plain Text)
  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:
Python Syntax (Toggle Plain Text)
  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
Similar Threads
Reputation Points: 2
Solved Threads: 30
Posting Whiz in Training
sravan953 is offline Offline
242 posts
since May 2009
Jul 5th, 2009
0

Re: Speed calculating algorithm?

Assuming you are using Python2, you could do it like this:
python Syntax (Toggle Plain Text)
  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
Reputation Points: 407
Solved Threads: 36
Posting Virtuoso
Lardmeister is offline Offline
1,701 posts
since Mar 2007
Jul 5th, 2009
0

Re: Speed calculating algorithm?

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.
Reputation Points: 186
Solved Threads: 77
Posting Pro in Training
shadwickman is offline Offline
495 posts
since Jul 2007
Jul 5th, 2009
0

Re: Speed calculating algorithm?

@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.
Reputation Points: 625
Solved Threads: 211
Posting Virtuoso
Ene Uran is offline Offline
1,704 posts
since Aug 2005
Jul 8th, 2009
0

Re: Speed calculating algorithm?

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.
Moderator
Reputation Points: 1333
Solved Threads: 1403
DaniWeb's Hypocrite
vegaseat is offline Offline
5,792 posts
since Oct 2004

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Python Forum Timeline: Help with columns formatting
Next Thread in Python Forum Timeline: wxPython download links broken?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC