笨方法学python18-20

# 习题 18:命名、变量、代码、函数
# 知识点总结:
# 一、函数可以给代码片段命名,就跟变量和数字命名一样
# 二、它们可以接受参数,就跟你的脚本接受argv一样。
# 三、通过使用#1和#2,它们可以让你创建微型脚本或者小命令。
#  this one is like your scripts with argv
# 用def定义函数print_two,参数为*args
def print_two(*args):
    # 将args解包,将所有的参数依次赋予左边的变量名
    arg1, arg2 = args
    # 打印包含格式化变量的字符串arg1: %r, arg2: %r ,变量名分别为arg1和arg2
    print("arg1: %r, arg2: %r" % (arg1, arg2))

# ok, that *args is actually pointless, we can just do this
# 用def定义函数print_two_again,参数分别为arg1和arg2
def print_two_again(arg1, arg2):
    # 打印包含格式化变量的字符串 arg1: %r, arg2: %r,变量名分别为arg1和arg2
    print("arg1: %r, arg2: %r" % (arg1, arg2))

# this just takes one argument
# 用def定义函数print_one,参数为arg1
def print_one(arg1):
    # 打印包含格式化变量的字符串 arg:1 %r,变量名为arg1
    print("arg:1 %r" % arg1)

# this one takes no arguments
# 用def定义函数print_none
def print_none():
    # 打印字符串I got mothin.
    print("I got mothin.")

# 调用函数print_two(),参数为字符串Zed,字符串Shaw
print_two("Zed", "Shaw")
# 调用函数print_two_again(),参数为字符串Zed,字符串Shaw
print_two_again("Zed", "Shaw")
# 调用函数print_one(),参数为字符串First!
print_one("First!")
# 调用函数print_none()
print_none()
"C:\Program Files\Python36\python.exe" E:/python3_project/ex18.py
arg1: 'Zed', arg2: 'Shaw'
arg1: 'Zed', arg2: 'Shaw'
arg:1 'First!'
I got mothin.

Process finished with exit code 0

自己在做这个联系的时候犯了一个错误就是"%r"命令的错误

def cheese_and_cracker(cheese_count,boxes of crackers):#会报错
def cheese_and_cracker(cheese_count,boxes_of_crackers):#正确代码

在这里插入图片描述

# 习题19:函数和变量 
# 创建名称为cheese_and_crackers函数,
# 参数分别为 cheese_count 和boxes_of_crackers
def cheese_and_crackers(cheese_count, boxes_of_crackers):
    # 打印包含格式化变量的字符串,变量名为cheese_count
    print("You have %d cheeses!" % cheese_count)
    # 打印包含格式化变量的字符串,变量名为boxes_of_crackes
    print("You have %d boxes of crackers!" % boxes_of_crackers)
    # 打印字符串 Man that's enough for a party!
    print("Man that's enough for a party!")
    # 打印字符串 Get a blanket并换行
    print("Get a blanket.\n")

# 打印字符串 We can just give the function numbers directly:
# 调用函数cheese_and_crackers,变量名分别是2030
print("We can just give the function numbers directly:")
cheese_and_crackers(20, 30)

# 打印字符串
# 将整数1050分别赋值给变量 amount_of_cheese和amount_of_crackers
print("OR, we can use variables from our script:")
amount_of_cheese = 10
amount_of_crackers = 50

# 调用函数 cheese_and_crackers 变量名分别是amount_of_cheese, amount_of_crackers
cheese_and_crackers(amount_of_cheese, amount_of_crackers)

# 打印字符串 We can even do math inside too:
# 调用函数cheese_and_crackers,变量名分别是整数1020的和,整数56的和
print("We can even do math inside too:")
cheese_and_crackers(10 + 20, 5 + 6)

# 打印字符串 And we can combine the two, variables and math
# 调用函数 cheese_and_crackers,
# 第一个变量名为变量amount_of_cheese 与整数 100的和
# 第一个变量名为amount_of_crackers 与整数1000的和
print("And we can combine the two, variables and math.")
cheese_and_crackers(amount_of_cheese + 100, amount_of_crackers + 1000)
"C:\Program Files\Python36\python.exe" E:/python3_project/ex19.py
We can just give the function numbers directly:
You have 20 cheeses!
You have 30 boxes of crackers!
Man that's enough for a party!
Get a blanket.

OR, we can use variables from our script:
You have 10 cheeses!
You have 50 boxes of crackers!
Man that's enough for a party!
Get a blanket.

We can even do math inside too:
You have 30 cheeses!
You have 11 boxes of crackers!
Man that's enough for a party!
Get a blanket.

And we can combine the two, variables and math.
You have 110 cheeses!
You have 1050 boxes of crackers!
Man that's enough for a party!
Get a blanket.


Process finished with exit code 0
# 习题 20: 函数和文件
# 通过from...inport...导入sys模组,将argv直接导入程序
from sys import argv
# 将argv解包,将所有的参数依次赋予左边的变量名
script, input_file = argv
# 用def定义函数print_all,参数为f
def print_all(f):
    # 在f上调用read()函数,无需任何参数。读取文本的内容并打印出来
    print(f.read())
# 用def定义函数rewind,参数为f
def rewind(f):
    # 在f上调用seek()函数
    f.seek(0)
# 用def定义函数print_a_line(),参数分别为line_count和f
def print_a_line(line_count, f):
    # 打印变量line_count.
    # 在f上调用readline()函数,无需任何参数。读取文本文件中的一行,并打印出来
    print(line_count, f.readline())
# open()函数用于打开文件,括号内为输入参数,参数为文件名,并存放到变量 current_file
current_file = open(input_file)
# 打印字符串 First let's print the whole file:\n
print("First let's print the whole file:\n")
# 调用函数print_all(),参数为current_file
print_all(current_file)
# 打印字符串 Now let's rewind, kind of like a tape.
print("Now let's rewind, kind of like a tape.")
# 调用函数 rewind(),参数为current_file
rewind(current_file)
# 打印字符串 Let's print three lines:
print("Let's print three lines:")
# 将整数1赋值给变量 current_line
current_line = 1
# 调用函数 print_a_line(),参数为current_line, current_file
print_a_line(current_line, current_file)
# 将变量 current_line 的值和整数1相加运算,并把结果存放到变量 current_line
current_line = current_line + 1
# 调用函数print_a_line(),参数为current_line, current_file
print_a_line(current_line, current_file)
# 将变量 current_line 的值和整数1相加运算,并把结果存放到变量 current_line
current_line = current_line + 1
# 调用函数print_a_line(),参数为current_line, current_file
print_a_line(current_line, current_file)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值