day10_Udemy Python 100days

    #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.
"""

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值