Python/Time, Distance, and Speed

Python is a scripting language and is therefore often used to automate boring or difficult activities such as calculating Time, Distance and Speed. This is an example of such a program.

Code

edit
Python
.


  1. "average_speed" is a variable (something that stores data, can be in various formats, such as a number)
  2. The "=" operator gives the variable on the left the value of what happens (what was returned) on the right
  3. The "int()" function converts what happens inside the brackets to a number and returns it.
  4. The "input()" function displays what is inside the brackets to the screen, and returns what the user inputs.
  5. The "print()" function takes whatever is returned inside of it and displays it on the screen
  6. The ' indicates that a string (letters using ASCII) are beings stored
  7. The "f" is used to indicate that a variable is used within the string
  8. The "{}" indicate that what happens within them is converted to a string, and then added to the external string
  9. "def" shows python where a function is being defined
  10. The "/" takes what is on the left, and divides it by the right, and returns a float (a number with decimals)
  11. "calc_distance" is the name of the function
  12. The "*" takes what is on the left and times it by what is on the right, returning it as an integer (a number with no decimal spaces)
  13. "(" don't have to have their ")" counter part on the same line
  14. ' ' ' shows a string that is on multepal lines
  15. "if" checks if the condition after it and between the ":" is true
  16. The name of a function, with brackets after it, is how you make a function run
  17. The "==" checks if the variable on the left is the same as the variable on the right, if that is the case, then it returns true.
  18. "elif" is executed when the all if (and elif) statements above it are false. It works based on weather the condition is true or false

How to use

edit

This piece of code can calculate the total distance, time and speed. If you run the code you first enter the values such as the total speed of a car, the time it took and the distance traveled. For example, if you want to find the speed you would enter:

  • The Distance Traveled
  • The Time (in hours) traveled
  • Select the speed formula