DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/index.php)
-   Python (http://www.daniweb.com/forums/forum114.html)
-   -   Speed calculating algorithm? (http://www.daniweb.com/forums/thread201758.html)

sravan953 Jul 5th, 2009 7:27 am
Speed calculating algorithm?
 
Hey guys,

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

This is the code I used:
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:
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

Lardmeister Jul 5th, 2009 11:57 am
Re: Speed calculating algorithm?
 
Assuming you are using Python2, you could do it like this:
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

shadwickman Jul 5th, 2009 1:58 pm
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 :P

Ene Uran Jul 5th, 2009 4:14 pm
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.

vegaseat Jul 8th, 2009 8:31 pm
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.


All times are GMT -4. The time now is 5:08 am.

Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC