|
|
5 years ago | |
|---|---|---|
| .. | ||
| solutions | 5 years ago | |
| README.md | 5 years ago | |
| README.pdf | 5 years ago | |
README.md
Python Programming
Python Conditionals: Practice Problems
In this homework, you're going to write code for two challenge problems.
You will practice these programming concepts we've covered in class:
- Declaring and using lists.
- Using the Python conditionals
if,elif, andelse. - Using
forandwhileloops.
Deliverables
Part of this homework will be code challenges and part will be reading with comprehension questions.
For the reading questions, make a text file called answers.txt and use it to compile your answers to all of the numbered questions.
For each of the code challenges listed below, you will create a new .py file and write code to solve the problem. For example, you would create problem1.py with your solution code to the first problem. Run the file from the command line to check your work.
Reminder: On your laptop, you can run the file from your command line with the following:
python problem1.py
Hint: Make sure you are printing something out with the
Requirements
By the end of this, you should have:
- Two different
.pyfiles (one for each code challenge). - One text file with answers to the five reading comprehension questions.
Code Challenges
Problem 1: IOU!
Skill you're practicing: Writing for loops to iterate over a list.
You have a list of Disney characters and you want to find out if each of them contain i, o, or u in their names. Loop through each character in the list and print out the following:
If the name contains a "u," print out the name plus "U are so Uniquely U!"
Otherwise if the name contains an "i," print out the name plus "I bet you're Impressively Intelligent!"
Otherwise if the name contains an "o," print out the name plus "O My! How Original!"
Otherwise, print the name plus "Ehh, a's and e's are so ordinary."
Starter Code
disney_characters = ["simba", "ariel", "pumba", "flounder", "nala", "ursula", "scar", "flotsam", "timon"]
Expected Output
simba I bet you're Impressively Intelligent!
ariel I bet you're Impressively Intelligent!
pumba U are so Uniquely U!
flounder U are so Uniquely U!
nala Ehh, a's and e's are so ordinary.
ursula U are so Uniquely U!
scar Ehh, a's and e's are so ordinary.
flotsam O My! How Original!
timon I bet you're Impressively Intelligent!
Hint: You can determine whether or not a string contains a particular character with an
ifstatement. For example,if "b" in my_string:will be true ifmy_stringcontains any b's.
Problem 2: If You're Cold, Sit in a Corner. It's 90 Degrees!
Skill you're practicing: Writing while loops.
Wow! It's 90 degrees Fahrenheit and you are sweating buckets! Luckily you have air conditioning, but it's really old and kind of finicky. It cools the room by three degrees and shuts off, so you have to keep turning it back on until the temperature gets to where you want it to be. Seventy five sounds much more pleasant than 90, so that's what you're shooting for.
While the temperature is greater than 75 degrees Fahrenheit, print "The temperature is XX — crank the AC!" and subtract 3 degrees from the temperature.
Once the temperature is cool enough and the loop is done, print "75. Ahh, that's better."
Starter Code
temperature = 90
Expected Output
Temperature is 90 — crank the AC!
Temperature is 87 — crank the AC!
Temperature is 84 — crank the AC!
Temperature is 81 — crank the AC!
Temperature is 78 — crank the AC!
75. Ahh, that's better.
Hint: Make sure that your loop conditional is being updated each iteration. Otherwise you'll end up with an infinite loop!
Reading Material
Read through the examples in these two articles about for loops and while loops from Digital Ocean. Then, answer the following questions.
-
What is a nested loop?
-
Which kind of loop is based on a conditional statement:
whileloops orforloops? -
When you want to iterate a specific number of times, would you typically use a
whileloop or aforloop? -
Is it possible to loop through a string one letter at a time? What is the example given in the article?
-
Extrapolate from what you learned in the articles: Do you think a
forloop be nested inside awhileloop? Why or why not?
All Done!
Time to get some rest!
