Python/Time, Distance, and Speed
< Python
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
editThe numbers after the "#" are keys, make sure to read what they say at the bottom of the page, as they will help you understand the code. |
Python
.
Key
edit- "average_speed" is a variable (something that stores data, can be in various formats, such as a number)
- The "=" operator gives the variable on the left the value of what happens (what was returned) on the right
- The "int()" function converts what happens inside the brackets to a number and returns it.
- The "input()" function displays what is inside the brackets to the screen, and returns what the user inputs.
- The "print()" function takes whatever is returned inside of it and displays it on the screen
- The ' indicates that a string (letters using ASCII) are beings stored
- The "f" is used to indicate that a variable is used within the string
- The "{}" indicate that what happens within them is converted to a string, and then added to the external string
- "def" shows python where a function is being defined
- The "/" takes what is on the left, and divides it by the right, and returns a float (a number with decimals)
- "calc_distance" is the name of the function
- 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)
- "(" don't have to have their ")" counter part on the same line
- ' ' ' shows a string that is on multepal lines
- "if" checks if the condition after it and between the ":" is true
- The name of a function, with brackets after it, is how you make a function run
- 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.
- "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
editThis 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