def repeat_message(message, times):
# Your code goes here
# Test your function
repeat_message("Hello world", 5)
Write a function max_of_three
that takes three numbers and returns the largest one.
def max_of_three(num_1, num_2, num_3):
# Your code goes here
# Test your function
a = int(input("First number: "))
b = int(input("Second number: "))
c = int(input("Third number: "))
result = max_of_three(a, b, c)
print(f"The max number was {result}"
Write a function that asks the user for "yes" or "no" input:
True
False
return get_yes_no()
def get_yes_no():
# Your code goes here
# Test your function
result = get_yes_no()
if result:
print(f"You chose 'yes'")
else:
print(f"You chose 'no'")
Write a function that displays a menu with 3 options and returns the user's choice (1, 2, or 3) as an integer (call int(...)
).
Ask again if not 1, 2, or 3
def get_menu_option(option_1, option_2, option_3):
# Your code goes here
option = get_menu_option("Add", "Subtract", "Divide")
if option == 1:
print("You chose Add")
elif option == 2:
print("You chose Subtract")
elif option == 3:
print("You chose Divide")
else:
print("Invalid option: '{option}'")
Write a function fibonacci that calculates the nth Fibonacci number. The Fibonacci sequence starts with 0 and 1, and each following number is the sum of the previous two.
def fibonacci(n):
# Your code goes here
# Hint: You can use a loop to calculate
# F(0) = 0, F(1) = 1
# F(2) = 1, F(3) = 2, F(4) = 3, etc.
# Or you can use the formal definition of a Fibonacci number:
# F(0) = 1, F(1) = 1, F(n) = F(n-1) + F(n-2)
# Test your function
print(fibonacci(0)) # Should print: 0
print(fibonacci(1)) # Should print: 1
print(fibonacci(5)) # Should print: 5
print(fibonacci(10)) # Should print: 55