Return multiple results
def find_capital():
f_name = input("What is the first name?")
l_name = input("What is the last name?")
return f_name[0],l_name[0]
names = find_capital()
print(f"First Capital is {names[0]}")
print(f"Second Capital is {names[1]}")
10.1 Leap Year Return
def is_leap(year):
if year % 4 == 0:
if year % 100 == 0:
if year % 400 == 0:
return "Leap year."
else:
return "Not leap year."
else:
return "Leap year."
else:
return "Not leap year."
def days_in_month(input_year, input_month):
month_days = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
is_leap_year = is_leap(input_year)
if is_leap_year == "Leap year.":
month_days[1] = 29
days = month_days[input_month-1]
return days
#🚨 Do NOT change any of the code below
year = int(input("Enter a year: "))
month = int(input("Enter a month: "))
days = days_in_month(year, month)
print(days)
Docstrings
Use 3 quotes around the string
def is_leap(year):
""" Returns whether this is a leap year or not """
if year % 4 == 0:
if year % 100 == 0:
if year % 400 == 0:
return "Leap year."
else:
return "Not leap year."
else:
return "Leap year."
else:
return "Not leap year."
# Writing is_leap( without the closing parenthesis should give you the docstring.
Another quick return example
def add(n1, n2):
return n1 + n2
def subtract(n1, n2):
return n1 - n2
def multiply(n1, n2):
return n1 * n2
def divide(n1, n2):
return n1 / n2
print(add(2, multiply(5, divide(8, 4))))
Don't use Returns before needed code
Returns will skip the rest of the code and return nothing or whatever it returns
# output of this is nothing, you'd think it might return "Terrible" since 25 < 40 but it dies at the return statement.
def my_function(a):
if a < 40:
return
print("Terrible")
if a < 80:
return "Pass"
else:
return "Great"
print(my_function(25))
The Calculator
Print vs Return
Why do we return instead of print? Reusability. If we have a function that returns the sum, we can then use that return to keep going. If we print it, it's pretty much final and not saved.
Art.py​
The logo. You can change it to do what you want to say, but I added my name to it :)
logo = """
_____________________
| _________________ |
| Solomon 0. | | .----------------. .----------------. .----------------. .----------------.
| |_________________| | | .--------------. || .--------------. || .--------------. || .--------------. |
| ___ ___ ___ ___ | | | ______ | || | __ | || | _____ | || | ______ | |
| | 7 | 8 | 9 | | + | | | | .' ___ | | || | / \ | || | |_ _| | || | .' ___ | | |
| |___|___|___| |___| | | | / .' \_| | || | / /\ \ | || | | | | || | / .' \_| | |
| | 4 | 5 | 6 | | - | | | | | | | || | / ____ \ | || | | | _ | || | | | | |
| |___|___|___| |___| | | | \ `.___.'\ | || | _/ / \ \_ | || | _| |__/ | | || | \ `.___.'\ | |
| | 1 | 2 | 3 | | x | | | | `._____.' | || ||____| |____|| || | |________| | || | `._____.' | |
| |___|___|___| |___| | | | | || | | || | | || | | |
| | . | 0 | = | | / | | | '--------------' || '--------------' || '--------------' || '--------------' |
| |___|___|___| |___| | '----------------' '----------------' '----------------' '----------------'
|_____________________|
"""
main.py​
from replit import clear
from art import logo
def add(n1, n2):
return n1 + n2
def subtract(n1, n2):
return n1 - n2
def multiply(n1, n2):
return n1 * n2
def divide(n1, n2):
return n1 / n2
operations = {
"+": add,
"-": subtract,
"*": multiply,
"/": divide
}
def calculator():
print(logo)
num1 = float(input("What's the first number?: "))
for symbol in operations:
print(symbol)
should_continue = True
while should_continue:
operation_symbol = input("Pick an operation: ")
num2 = float(input("What's the next number?: "))
calculation_function = operations[operation_symbol]
answer = calculation_function(num1, num2)
print(f"{num1} {operation_symbol} {num2} = {answer}")
if input(f"Type 'y' to continue calculating with {answer}, or type 'n' to start a new calculation: ") == 'y':
num1 = answer
else:
should_continue = False
clear()
calculator()
calculator()