#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")
day8_Udemy Python 100days
最新推荐文章于 2024-08-19 13:32:52 发布