COS 100: Introduction to Programming
Interim 2022
Lab 1: Working with numbers
In-class practice labs will not directly impact your grade (although you'll get your standard participation point for staying on task), and will serve to help you with homework projects.A few words of advice:
- You are writing programs now, so you should do so in the Editor, not the Shell.
- You don't have to turn these in, but you certainly should keep your work for reference later.
-
We will have a lot of labs, so you might want to name your files
lab1a.py
andlab1b.py
, or something like that, to be more organized. - Try to do things yourself (you'll learn the most), but don't struggle for too long; if you are stuck, ask for help!
Lab 1A: Average of three numbers
- Task: Collect three (3) integers from the user with separate prompts for each input, and then print out the average of these three numbers (which may require a floating point number even though the inputs were integers). Look at the sample run below for formatting.
- Make sure you provide good prompts to the user.
- Ask the professor or TA for help if you get stuck.
- When you think you have it correct, call over the TA or professor to take a look at your code.
- Move on to the next lab.
-
Sample run:
Please enter your first integer: 4 Please enter your second integer: 7 Please enter your third integer: 14 The average of 4, 7 and 14 is 8.333333 Farewell!
Lab 1B: Sales tax
- Task: Collect a money amount from the user, and calculate sales tax for their purchase.
- Minnesota has a 6.875% sales tax.
- Make sure you treat your percentage correctly. Many people will be off by a factor of 100 on their first try. Look at your output and ask yourself if it makes sense.
- Make sure you provide good prompts to the user.
- Provide the user with their sales tax, as well as the final total amount. See the sample run for formatting guidance.
-
You don't have to worry about rounding to the nearest penny for now.
- If you're feeling really ambitious, you can look into Python string formatting.
- Formatting the text like this is entirely optional for this practice lab. It involves several concepts that we haven't covered yet, so it's really only for those of you who want to go the extra mile.
- When you think you have it done, call over the TA or professor to take a look. Once you have gotten a thumbs up for both practice labs, you should be all set to complete your next programming project!
-
Sample run:
Please enter your purchase total: 100 For a purchase of $100.0, your sales tax is $6.875000000000001 for a total of $106.875 Have a good day. Come again soon!
Challenge Lab 1C: Chopping off the head
This lab is for those who want more of a challenge. If you're not able to get this one right today, don't worry. It is unrelated to your homework project. However, there are several ways to accomplish the task below, including some that you could do now with some fancy math, and some that involve coding concepts we haven't discussed yet.- Task: Ask the user for a positive integer (and you can assume they will enter in a legitimate positive integer). Tell the user what this number is equal to if you remove the first digit.
-
You may find some of the math library functions helpful.
See the official Python 3 documentation.
For instance, note that there's a function named
sqrt()
, and if you wanted to take the square root of a number, you could do so by including the lineimport math
at the beginning of your program, and using code likemath.sqrt(100)
, which would evaluate to the value10.0
. (Square roots probably aren't helpful for this problem, but that's an example of what the built-in math library function can do in Python.) -
Sample runs:
Please enter a positive integer: 23 Without the first digit, that is equal to 3 Please enter a positive integer: 100 Without the first digit, that is equal to 0 Please enter a positive integer: 55112 Without the first digit, that is equal to 5112 Please enter a positive integer: 1024 Without the first digit, that is equal to 24