Udemy - 100 Days of Code: The Complete Python Pro Bootcamp
Day 10 - Beginner - Functions with Outputs
73. Functions with Outputs
# Define a Function
def format_name(first_name, last_name):
formatted_first_name = first_name.title()
formatted_last_name = last_name.title()
return f"{formatted_first_name} {formatted_last_name}"
# Call a Function
output = format_name("AnGeLa", "YU")
print(output)
74. Multiple return values
# Conditional Returns 条件返回
def can_buy_alcohol(age):
if age >= 18:
return True
else:
return False
# Empty Returns 空返回
def can_buy_alcohol(age):
if type(age) != int:
return
if age >= 18:
return True
else:
return False
75. Docstrings
def format_name(first_name, last_name):
"""Take a first and last name and format it
to return the title case version of the name."""
formatted_first_name = first_name.title()
formatted_last_name = last_name.title()
return f"{formatted_first_name} {formatted_last_name}"
output = format_name("AnGeLa", "YU")
print(output)
76. Calculator Project
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():
should_accumulate = True
num1 = float(input("What is the first number?: "))
while should_accumulate:
for symbol in operations:
print(symbol)
operation_symbol = input("Pick an operation: ")
num2 = float(input("What is the next number?: "))
answer = operations[operation_symbol](num1, num2)
print(f"{num1} {operation_symbol} {num2} = {answer}")
choice = input(f"Type 'y' to continue calculating with {answer}, or type 'n' to start a new calculation: ")
if choice == "y":
num1 = answer
else:
should_accumulate = False
print("\n" * 20)
calculator()
calculator()
1079

被折叠的 条评论
为什么被折叠?



