笨方法学Python18-26

#P18 命名、变量、代码和函数
#this one is like your scripts with argv
def print_two(*args):
    arg1,arg2 = args
    print(f"arg1:{arg1},arg2:{arg2}")
#ok. that *args is actually pointless,we can just do this

def print_two_again(arg1,arg2):
    print(f"arg1:{arg1},arg2:{arg2}")

#this just take one argument
def print_one(arg1):
    print(f"arg1:{arg1}")

#this one take no arguments
def print_none():
    print("I got nothing")

print_two("Cao","Zhe")
print_two_again("haha","xixi")
print_one("Hey")
print_none()
#19
def cheese_and_crackers(cheese_count,box_of_vrackers):
    print(f"You have {cheese_count} cheese!")
    print(f"you have {box_of_vrackers} boxes of crackers")
    print("Man that's enough for a party")
    print("get a blanket.\n")

print("we can just give the function numbers directly")
cheese_and_crackers(20,30)

print("we can use variables from our script:")
amount_of_cheese = 10;
amount_of_crackers = 50;

cheese_and_crackers(amount_of_cheese,amount_of_crackers)

print("we can even do math inside too:")
cheese_and_crackers(10+20,5+6)

print("And we can combine the two, variables and math:")
cheese_and_crackers(amount_of_cheese+100,amount_of_crackers+1000)

print("we can put them by ourselves:")
cheese_and_crackers(int(input("起司")),int(input("篮子")))
#20
from sys import argv

script,input_file = argv
def print_all(f):
    print(f.read())
def rewind(f):
    f.seek(0)
def print_a_line(line_count,f):
    print(line_count,f.readline())
current_file = open(input_file)

print("First let's print the whole file:\n")
print_all(current_file)
print("Now let's rewind, kind of like a type.")
rewind(current_file)
print("Let's print three lines:")

current_line = 1
print_a_line(current_line,current_file)
current_line += 1
print_a_line(current_line,current_file)
current_line += 1
print_a_line(current_line,current_file)

rewind(current_file)
print(current_file.readline())
print(current_file.readline())
print(current_file.readline())

#解决间隔空行
rewind(current_file)
print(current_file.readline(),end="")
print(current_file.readline(),end="")
print(current_file.readline(),end="")
#21
def add(a,b):
    print(f"ADDING {a}+{b}")
    return a+b
def subtract(a,b):
    print(f"Subtracting {a}-{b}")
    return a-b
def multiplay(a,b):
    print(f"Multiplaying {a}*{b}")
    return a*b
def divide(a,b):
    print(f"Dividing {a}/{b}")
    return a/b

print("Let's do some math with just functions")

age = add(30,5)
height = subtract(78,4)
weight = multiplay(90,2)
iq = divide(100,2)

print(f"Age: {age}, Height: {height}, Weight: {weight},IQ: {iq}")

#a puzzle for the extra credit,type it in anyway.
print("here is a puzzle")

Result = add(age,subtract(height,multiplay(weight,divide(iq,2))))
print("That become:",Result,"can you do it by hand?")
#23
import sys
script,encoding,error = sys.argv

def main(language_file,encoding,errors):
    line = language_file.readline()

if line:
    print_line(line,encoding,errors)
    return main(language_file,encoding,errors)

def print_line(line,encoding,errors):
    next_lang = line.strip()
    raw_bytes = next_lang.encode(encoding,errors = errors)
    cooked_string = raw_bytes.decode(encoding,errors = errors)
    print(raw_bytes,"<==>,"cooked_string)

language = open("languages.txt",encoding= "utf-8")

main(languages,encoding,error)

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值