![]() |
| ||
| 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")But, my question is, can't it be done like this: s=input("Speed: ")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 |
| ||
| 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:") |
| ||
| 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 |
| ||
| 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. |
| ||
| 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