Reading: Dictionaries and While Loops
Pre Class Reading Assignment
On the O'Reilly website read Chapter 6 in Python Crash Course, 3rd Edition and Chapter 7, the section on "How the input() Function Works" and "Introducing while Loops." Don't bother with the other sections in Chapter 7.
Here is a direct link to the readings: PCC Chapter 6: Dictionaries PCC Chapter 7: User Input and While Loops
Remember that you will have to sign in to your free account that you created earlier.
Things to Look Out For
- Dictionaries use braces {} as opposed to lists which use square brackets [].
- Keys and values are separated by colons, and key-value pairs are separated by commas.
- A dictionary's keys cannot be lists, but its values can be.
- What is the input statement used for?
- How do you format a prompt question if it doesn't fit on its initial line?
- What is the difference between for loops and while loops?
- How would you code a stopping point if you wanted to stop the input program?
- While loops will only run while a condition is true, and will stop when the condition is false.
Pre-Class Quiz Challenge
Rivers with user input and while loops: Create a program that builds a dictionary of rivers and the countries they run through, using user input.
- Create a new Colab notebook.
- Title the notebook "(Your_Name)_Pre_Dict_While.ipynb".
- Start with an empty dictionary called rivers.
- Use a while loop to repeatedly ask the user for:
- The name of a river
- The country it runs through
- Store these as a key-value pair in the dictionary.
- After each entry, ask the user if they want to add another river. Stop when they say "no".
- Use a while loop to go through the dictionary and print a sentence for each river, such as:
The Nile runs through Egypt.
- Use another while loop to print only the names of the rivers.
- Use another while loop to print only the names of the countries.
Turning in/Rubric
REMINDER - For this class, you will only turn in the links to your colab notebooks. You will get a 0 for this assignment if you turn in a python file or a link that is not correct, wrong assignment, or does not give editor permission.
Rubric:
| Item | Points Possible |
|---|---|
Total |
3 |
The following is not a part of the rubric, but specifies how you can lose points. For example: if you fail to share your link correctly.
| Reasons for Points Lost | Amount |
|---|---|
| Link shared incorrectly | -10% |