#More Functions
#Functions with output
def format_name(f_name,l_name):
print(f_name.title())
print(l_name.title())
format_name("angela","ANGELA")
def format_name(f_name,l_name):
formated_f_name = f_name.title()
formated_l_name = l_name.title()
print(f"{formated_f_name} {formated_l_name}")
def format_name(f_name,l_name):
formated_f_name = f_name.title()
formated_l_name = l_name.title()
return f"{formated_f_name} {formated_l_name}"
formated_string = format_name("AngEla","YU")
print(formated_string)
print(format_name("AngEla","YU"))
output = len("Angela")
"""
This line of code is going to run this built in len function,
calculating the number of letters inside this input that we've given it.
And then it's going to return the result as an output,which we can now capture inside a variable.
"Angela" is the inputs,len is the function,
once len("Angela") is completed,the return or whatever it is after the return statement
is going to replace len("Angela")
That result or output then gets stored inside of this variable.
"""
#Multiple return values
def format_name(f_name,l_name):
formated_f_name = f_name.title()
formated_l_name = l_name.title()
return f"{formated_f_name} {formated_l_name}"
print("This got printed")
#This code doesn't ever get executed,because the return tells the computer
# that this is the end of function and you should now exit the function.
def format_name(f_name,l_name):
formated_f_name = f_name.title()
formated_l_name = l_name.title()
return f"Result:{formated_f_name} {formated_l_name}"
print(format_name(input("What is your first name?"),
input("What is your last name?")))
#input: output:Result
def format_name(f_name,l_name):
if f_name == "" or l_name == "":
return "You didn't provide valid inputs."#escape the function
formated_f_name = f_name.title()
formated_l_name = l_name.title()
return f"{formated_f_name} {formated_l_name}"
print(format_name(input("What is your first name?"),
input("What is your last name?")))
#day 10.1 Days in Month
def is_leap(year):
if year % 4 == 0:
if year % 100 == 0:
if year % 400 == 0:
return True
else:
return False
else:
return True
else:
return False
def days_in_month(year,month ):
if month > 12 or month < 1:
return "Invalid month"
month_days = [31,28,31,30,31,30,31,31,30,31,30,31]
if is_leap(year) and month == 2:
return 29
return month_days[month - 1]
year = int(input("Enter a year:"))
month = int(input("Enter a month:"))
days = days_in_month(year,month)
print(days)
#Docstrings
"""
This is a way to be able to start documenting functions
and giving each function a little bit of an explainer.
The docstring has to go as the first line after the declaration.
"""
#You can also use this as a multiline comment
def format_name(f_name,l_name):
"""Take s first and last name and format it
to return the title case version of the name."""
formated_f_name = f_name.title()
formated_l_name = l_name.title()
return f"Result:{formated_f_name} {formated_l_name}"
#Calculator Program
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)
"""Our function all have a return value,so calculation_function(num1,num2)
is going to be replaced when the code runs with the actual answer.
So we save that inside a variable called answer."""
print(f"{num1} {operation_symbol} {num2} = {answer}")
if input(f"Type 'y' to continue calculating with {answer},or type 'n' to start a new caculation:") == "y":
num1 = answer
else:
should_continue = False
calculator()
calculator()
"""This recursion(递归) basically happens because we're calling this calculato function
within the calculator function.
#Infinite loop
def calculator():
print("aaaaa")
calculator()
calculator()
Be careful and make sure that there is some sort of condition
that needs to be met in order for this function to call itself.
"""
"""
# The difference between the print and the return
operation_symbol = input("Pick another operation:")
num3 = int(input("What's the next number?:"))
calculation_function = operations[operation_symbol]
# second_answer = calculation_function(calculation_function(num1,num2),num3)
# calculation_function(num1,num2) != answer,because operation_symbol = '*'
second_answer = calculation_function(answer, num3)
print(f"{answer} {operation_symbol} {num3} = {second_answer}")
"""
"""function provides outputs
you can take the output from one function call and pass it
into next function call as an input.
This output can be reused in different parts of the code.
This not only allows us to reduce repetition and also
make our more reusable and more modular,but also
it give us the flexibility of performing more actions
after a function has completed.
"""
day10_Udemy Python 100days
最新推荐文章于 2024-08-19 14:41:29 发布