0

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

7
Contributors
6
Replies
15
Views
7 Years
Discussion Span
Last Post by Dani
Featured Replies
  • 1
    Dani 1,544   2 Weeks Ago

    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 … Read More

0

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
0

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

0

@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.

0

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

1

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.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.