day8_Udemy Python 100days

    #More function
#Simple function
def greet():
    print(f"Hello ANgela")
    print(f"How do you do ?")

greet()

#Function with allows for input

def greet_with_name(name):
    print(f"Hello {name}")
    print(f"How do you do {name}?")

greet_with_name("Angela")


#The parameter is the name of data that's being passed in
#The argument is the actual value of the data


    #Functions with more than 1 input
def greet_with(name,location):
    print(f"Hello {name}")
    print(f"What is it like in {location}")

greet_with("Angela","nowhere")

greet_with(location="nowhere",name="Angela")


#Positional Arguments
def my_function(a,b,c):

my_function(1,2,3)
my_function(2,3,1)

#Keyword Arguments
my_function(a=1,c=3,b=2)



    #Day8.1 Area calc
import math

def paint_calc(height, width, cover):
    area = height*width
    num_of_cans = math.ceil(area/cover)
    print(f"You'll need {num_of_cans} cans of paint.")

test_h = int(input("Height of wall:"))
test_w = int(input("Width of wall"))
coverage = 5
paint_calc(height=test_h, width=test_w, cover=coverage)


    #8.2 Prime_number


    #Caesar Cipher
alphabet = ['a','b','c','d','e','f','g','h','i','j','k','l','m'
            ,'n','o','p','q','r','s','t','u','v','w','x','y','z',
            'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k','l',
            'm','n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'
            ]

def caesar(start_text,shift_amount,cipher_direction):
    end_text = ""
    if cipher_direction == "decode":
        shift_amount *= -1
    for char in start_text:#char 包括 空格、符号、字母
        if char in alphabet:
            position = alphabet.index(char)
            new_position = position + shift_amount
            new_char = alphabet[new_position]
            end_text += new_char
        else:
            end_text += char
    print(f"The encoded text is {end_text}")

from art import logo
print(logo)

should_end = True
while should_end:

    direction = input("Type 'encode' to encrypt,type 'decode' to decrypt:\n")
    text = input("Type your message:\n").lower()
    shift = int(input("Type the shift number:\n"))

    shift = shift % 26#用户输入的shift可能超过list:alphabet范围,故取余数

    caesar(start_text=text,shift_amount=shift,cipher_direction=direction)

    restart = input("Type 'yes' if you want to go again.Otherwise type 'no'.\n")
    if restart == 'no':
        should_end = False
        print("Goodbye")

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值