COS 100: Introduction to Programming
Interim 2022
Lab 3: Using logic
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.
Lab 3A: Wie finden Sie das Wetter?
-
Goal:
To get experience with nested
if
statements and compound conditional statements.
-
Task:
Ask the user if it is cold outside, then ask them if they are tired.
- If it is cold and they are tired, tell them to stay inside and read a book.
- If it is cold and they are not tired, tell them to go skiing.
- If it is not cold and they are tired, tell them to take a nap outside.
- If it is not cold and they are not tired, tell them to go running.
-
You should try to write this program in two different ways:
- Write it once with nested
if
statements with simple
conditionals.
- Write it again with non-nested
if
statements that use
logical and
and/or logical or
operators to create
more complicated conditionals.
-
Hints:
- Remember that strings in Python can be compared using
==
.
- Remember to give the user good prompts.
Lab 3B: Do you know facts about my pet?
-
Task:
Ask the user for a type of pet and whether their pet is male or female.
Then we will tell them facts about their pet.
-
Ask the user only two questions total: "What type of animal is your pet?" and "Is your pet male or female?"
-
We'll assume that the types of pets are: cat, dog, fish, bird, hamster, and snake.
-
Facts you should give to the user if they select the appropriate animal:
- cats, dogs, and hamsters are all mammals.
- cats, dogs, and snakes eat meat.
- hamsters are omnivores.
- fish and snakes both have scales.
- birds, hamsters, and snakes all live in cages.
- female birds, fish, and snakes lay eggs.
-
Only provide the fact if the animal the user provides fits in that category.
-
For example, if the user says they have a snake that is female, you should tell them:
- Snakes eat meat
- Snakes have scales
- Snakes live in cages
- Female snakes lay eggs
-
Challenge:
Format your facts in a single sentence, like:
"Female snakes eat meat, have scales, live in cages, and lay eggs."
Can you always get the punctuation and the use of the word "and" correct no matter what the user says?
-
Hint:
You can use
print("Tofu", end="")
to print the word Tofu
and stay on the same line for the next print
statement.
Challenge Lab 3C: Is there a better way to do this?