|
|
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 to solve a few problems.
You will practice these programming concepts we covered in class:
- Declaring and using variables.
- Using mathematical operators.
- Implementing Python conditionals:
if,elif, andelse.
Deliverables
The first part of this homework is a mini-quiz. You can put all of your answers for that in a single text file.
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 for 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 command:
python problem1.py
Hint: Make sure you are printing something out with the
Requirements:
By the end of this, you should have:
- One text file for your mini-quiz questions.
- Two different
.pyfiles (one for each coding problem).
Part 1: Mini-Quiz
Put your solution into a file called quiz.txt.
-
True or false: An
ifstatement can live without anelsestatement. -
True or false: An
elsestatement can live without anifstatement. -
True or false: An
elifstatement can live without anifstatement. -
True or false: An
if+elifstatement can live without anelsestatement. -
True or false: The following statements are equivalent.
if a == True:
if a:
- In the following code block, what prints?
a = True
b = True
c = False
if a:
print("1")
if b:
print("2")
if c:
print("3")
if a and b:
print("4")
if a and c:
print("5")
if b and c:
print("6")
if a or b:
print("7")
if a or c:
print("8")
if b or c:
print("9")
- In the following code block, what prints?
a = False
b = True
c = True
if a:
print("1")
elif b:
print("2")
elif c:
print("3")
else:
print("4")
Part 2: Code Challenges
Problem 1: Most Clocks Are Normal, But Some Are Cuckoo!
Skill you're practicing: Writing conditionals.
For this problem, put your solution into a file called problem1.py.
You're making a program for your coworkers that displays a message on their desktop's idle screen. Depending on the time of day, you want to print a different quote. You'll create a variable, time, which has an integer value between zero and 23, representing the hour of the day in military time, which is a 24-hour clock. Write a conditional statement with Python code that prints exactly one message using the following rules:
If the time of day is before 9 a.m. --> print the quote "Morning is wonderful. Its only drawback is that it comes at such an inconvenient time of day."
Otherwise if the time of day is before or exactly 4 p.m. --> print the quote "Working hard or hardly working?"
Otherwise, if the time of day is before 8 p.m. --> print the quote "How did it get so late so soon?"
Otherwise if the time of day is before 10 p.m. --> print the quote "A day without sunshine is like, you know, night."
Otherwise, for any time 10 p.m. or later --> print the quote "Burning the midnight oil!"
Starter Code
time = 8
Hint: Test your code with different values for time of day. Make sure you are only printing one statement, regardless of the value of
time!
Problem 2: I Came, I 'Saur, I Conquered
Skill you're practicing: Writing conditionals with multiple conditions.
For this problem, put your solution into a file called problem2.py.
The mighty Tyrannosaurus rex is the king of dinosaurs, and he does whatever he pleases. When he's hungry, he eats. When he's angry, he fights. When he's bored, he roars. When he's tired, he sleeps! Write a conditional statement that selects one of the following actions for T-Rex based on his current mood. T-Rex's actions are ordered by precedence:
If T-Rex is angry, hungry, and bored he will eat the Triceratops.
Otherwise if T-Rex is tired and hungry, he will eat the Iguanadon.
Otherwise if T-Rex is hungry and bored, he will eat the Stegasaurus.
Otherwise if T-Rex is tired, he goes to sleep.
Otherwise if T-Rex is angry and bored, he will fight with the Velociraptor.
Otherwise if T-Rex is angry or bored, he roars.
Otherwise T-Rex gives a toothy smile.
Starter Code
angry = True
bored = True
hungry = False
tired = False
# Example `if` statement:
if bored:
print("T-Rex roars! Rawr!")
All Done!
Now, go be like a T-Rex and do what you please!
