A1 - Python Review
Open Visual Studio Code:
- Install the Python extension (I'll show you how to do this)
- Open a pre-existing folder you have for this course, either in your Google Drive (if you have Google Drive Desktop installed) or in your
T:drive. - Create a file in that folder called
python_review.py - Paste in the following code:
def main_menu():
print("1. Answer a question")
print("2. Try the calculator")
choice = input("What would you like to do? ")
while choice != "1" and choice != "2":
choice = input("Invalid input. Try again: ")
if choice == "1":
answer_question()
if choice == "2":
calculator()
def answer_question():
# TODO: Replace the contents of this function with your own question,
# and include responses in the same format as in this example
# (using if/elif/else)
reply = input("Will you have homework today? ")
if reply == "yes":
print("Too bad!")
elif reply == "no":
print("Enjoy some free time!")
elif reply == "maybe":
print("Hopefully not!")
else:
print("I didn't understand your answer.")
def calculator():
num1 = int(input("What is the first number? "))
num2 = int(input("What is the second number? "))
# TODO: If the user selects an invalid choice here, continue asking them again until
# they provide a valid answer. Use a `while` loop.
# Check out the main_menu function for a similar example.
choice = input("Select an operation: (a)dd / (s)ubtract / (m)ultiply / (d)ivide: ")
if choice == "a":
print(num1+num2)
# TODO: Similar to `add`, create functions and function calls for `subtract`, `multiply` and `divide`
main_menu()
Take some time to look over the code, and run the program several times to try out the various scenarios it offers.
Once you are comfortable with everything going on in the program, find each comment marked with # TODO, and complete the task as indicated in the comment