Day 10 - Udemy - 100 Days of Python 学习笔记

Python3.8

Python 是一种高级、解释型、通用的编程语言,以其简洁易读的语法而闻名,适用于广泛的应用,包括Web开发、数据分析、人工智能和自动化脚本

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

Demo

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()

您可能感兴趣的与本文相关的镜像

Python3.8

Python3.8

Conda
Python

Python 是一种高级、解释型、通用的编程语言,以其简洁易读的语法而闻名,适用于广泛的应用,包括Web开发、数据分析、人工智能和自动化脚本

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值