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'

    print (s)
    print (d)
    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'?


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():

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

also remember that in Python2 ' /' is an integer division by default.
For instance 12/15 gives you a zero. This has changed in Python3.

help me how do i make a program that will do find the time wether you put o.5 hours or 1 hour because i can't make a program that does both

Welcome to DaniWeb, Hiiiii!! Please post your question as a new thread instead of hijacking this seven year old discussion. It seems you missed where it said "This question has already been answered. Start a new discussion instead." ... Also, please be sure to show what you've done so far and where you're stuck. We won't just do your homework for you.