The basic logic operations in Python are and, or, and not. Refer to the following videos for review.
The truth tables for the logic operations are:
| a | b | a and b |
|---|---|---|
| True | True | True |
| True | False | False |
| False | True | False |
| False | False | False |
| a | b | a or b |
|---|---|---|
| True | True | True |
| True | False | True |
| False | True | True |
| False | False | False |
| a | not a |
|---|---|
| True | False |
| False | True |
For each of the problems below, think about the answer before clicking the "Reveal Answer" button.
Given the following code, what code would you write to print True if temp is greater than 60 and less than 80?
temp = 75
print(temp > 60 and temp < 80)Complete the following code with an if statement so that if either "Frank" or "Joe" is entered, it prints "Hardy Boy".
name = input("Enter a name: ")
if name == "Frank" or name == "Joe":
print("Hardy Boy")This asks if you are going to carry an umbrella. It then randomly decides if it is going to rain. Complete the code so that if it raining and you have an umbrella, it prints "You stay dry!", but if it is raining and you don't have an umbrella, it prints "You get wet!". If it is not raining, it prints "No rain today!"
from random import randint
umbrella = input("Are you going to carry an umbrella? ")
if randint(0, 1) == 1:
raining = True
else:
raining = False
if umbrella == "yes" and raining == True:
print("You stay dry!")
elif umbrella == "no" and raining == True:
print("You get wet!")
elif raining == False:
print("No rain today!")You are to write a program called "Adventure Prep" where you ask several questions to determine if the user is ready for an adventure. You should ask at least three questions that can be answered with "yes" or "no". Based on the answers, you should print out a message telling the user if they are ready for an adventure or not. Use one or more of the logical operators and, or, and not somewhere in your program.
Here is an example of how the program might work:
Welcome to Adventure Prep! Are you carrying a map? yes Do you have enough food? yes Is your backpack packed? no You are not ready for an adventure. Make sure to pack your backpack!
It is your decision what kind of adventure to go on and what is needed to be prepared. Your adventure could be a camping trip, a knight's quest, a space mission, or anything else you can think of!
Make sure to test your program with different combinations of answers to make sure it works correctly.
Save your program as lastname_prep.py and upload it to OneDrive. It is due at the end of class on Thursday, February 5th.
Good luck and have fun!