Sep 02: Strings and Arithmetic Expressions
Room: King Hall 250
Activities¶
- Slides - Strings and Expressions
- Lab exercise on arithmetic
Lab - Arithmetic¶
This activity will help you practice some Python arithmetic basics.
Specifically you will practice:
- Converting user input to either a floating point number (float), like 3.14159, or an integer (int), like 314159.
- Using floating point division
/
, integer division//
, and the remainder, or modulo, operator%
in the appropriate situations.
Problem 1: Splitting feet into miles, furlongs, and feet.¶
For this problem you are going to write a program called feet_conversion.py
to use integer division and remainders to take a total number of feet and split it into miles, furlongs, and feet.
- 1 mile is 5280 feet
- 1 furlong is 660 feet (it’s some old English measure nobody uses anymore, but let’s use it anyway!).
- For example, 12345 feet is 2 mile(s), 2 furlong(s), and 465 feet.
First: Figure out how to solve the problem yourself by hand on paper. You can’t code something you don’t know how to solve.
Second: You will probably need to use the integer division //
and the remainder %
operators.
Hint: You probably want a variable for the remaining feet. For instance, before you’ve figured out the example above, the remaining feet is 12345. But once you figure out that there are two full miles in 12345 feet (2 full miles is 10560 feet), then the remaining feet you have to deal with in your calculation is 1785 feet. Then how many full furlongs are in 1785 feet? Well, 2 again, since
660 × 2 = 1320
And finally after we remove those 1320 from 1785 the remaining feet is 465. Notice how close the words remaining and remainder are.
Your program's input and output should be formatted exactly as shown below:
Enter a total number of feet: 12345
12345 total feet is 2 mile(s), 2 furlong(s), and 465 feet.
feet_conversion.py
, please submit it to gradescope.
Problem 2: Splitting seconds into hours, minutes, and seconds.¶
This problem is similar to the previous one. Think about the problem and develop an algorithm before starting:
Input | Output | ||
---|---|---|---|
Total Seconds | Hours | Minutes | Seconds |
5000 | 1 | 23 | 20 |
3625 | 1 | 0 | 25 |
1000 | 0 | 16 | 40 |
Create a new program called seconds.py
in Thonny.
Your program should display the prompt Enter the number of seconds:
exactly as shown and take the input from the user.
Using the value entered by the user, your program must calculate the equivalent hours, minutes, and seconds using the algorithm you developed.
Finally, your program should output the result. The format must be EXACTLY as shown, including whitespace.
For example:
Enter the number of seconds: 1000
1000 seconds = 0 hours, 16 minutes, and 40 seconds
\n
). Incorrect spacing or anything other than three lines of output (each with one newline character at the end) will cause the output to be flagged as incorrect, even if the math is correct.
When you've completed the program called seconds.py
, please submit it to gradescope.