Day3
- 习题 13: 参数、解包、变量
from sys import argv script, first, second, third = argv print("The script is called:",script) print("Your first variable is:",first) print("Your second variable is:",second) print("Your third variable is:",third) #运行power shell #cd E:\py #似乎是只能进到一级文件夹 #python 13.py first 2nd 3rd
powershell 运行结果
第 3 行将 argv “解包(unpack)”,与其将所有参数放到同一个变量下面,我们将
每个参数赋予一个变量名: script, first, second, 以及 third。这也许看上
去有些奇怪, 不过”解包”可能是最好的描述方式了。它的含义很简单:“把 argv
中的东西解包,将所有的参数依次赋予左边的变量名”。
-
习题 14: 提示和传递
1 from sys import argv 2 3 script,user_name = argv 4 prompt = '> ' 5 6 print("Hi %s ,I'm the %s script." % (user_name,script)) 7 print("I'd like to ask you a few questions.") 8 print("Do you like me %s?" % user_name) 9 likes = input(prompt) 10 11 print("Where do you live %s?" % user_name) 12 lives = input(prompt) 13 14 print("What kind computer do you have?") 15 computer = input(prompt) 16 17 print(""" 18 Alright, so you said %s about liking me. 19 You live in %s. Not sure where what is. 20 And you have a %s computer.Nice. 21 """ % (likes, lives, computer))
注意:
print("%r %s" % (12, 25))
多个格式化字符,记得要在print 的括号里,而且还要一个括号括起来
-
习题 15: 读取文件
1 from sys import argv 2 3 script, filename = argv 4 5 txt = open(filename) 6 7 print("Here's your file %r:" % filename) 8 print(txt.read()) 9 10 print("Type the filename again:") 11 file_again = input("> ") 12 txt_again = open(file_again) 13 14 print(txt_again.read())
-
习题 16: 读写文件
1 from sys import argv 2 3 script, filename = argv 4 5 print("We are going to erase %r." % filename) 6 print("If you don't want that, hit CTRL-C(^C).") 7 print("If you do want that, hit RETURN.") 8 9 input("?") 10 11 print("Opening the file...") 12 target = open(filename, 'w')#以写模式打开文件,其实会新建一个文件,若原来有,则会被这个覆盖 13 14 print("Truncating the file. Goodbye!") 15 target.truncate() 16 17 print("Now I'm going to ask you for three lines.") 18 19 line1 = input("line 1:") 20 line2 = input("line 2: ") 21 line3 = input("line 3: ") 22 23 print("I'm going to write these to the file.") 24 25 target.write(line1) 26 target.write("\n") 27 target.write(line2) 28 target.write("\n") 29 target.write(line3) 30 target.write("\n")
#用一行写出来: 31 #target.write(line1+"\n"+line2+'\n'+line3)
32 print("And finally, we close it.") 33 target.close()
-
习题 17: 更多文件操作
1 from sys import argv 2 from os.path import exists 3 4 script, from_file, to_file = argv 5 6 print("Copying from %s to %s" % (from_file, to_file)) 7 8 #we could do these two on one line too, how? 9 input1 = open(from_file) 10 indata = input1.read() 11 12 print("Does the output file exist? %r" % exists(to_file)) 13 print("Ready,hit RETURN to continue, CTRL-C to abort.") 14 input() 15 16 output = open(to_file,'w') 17 output.write(indata) 18 19 print("Alright, all done.") 20 21 output.close() 22 input1.close()
第一次报错因为17行,open的时候没有指定以写模式打开,记住,open(file,'w') 要指定打开模式
-
习题 18: 命名、变量、代码、函数
1 #this one is like your script with argv 2 def print_two(*args): 3 arg1, arg2 = args 4 print("arg1: %r, arg2: %r" % (arg1, arg2)) 5 6 #ok, that *args is actually pointless, we can just do this 7 def print_two_again(arg1, arg2): 8 print("arg1: %r, arg2: %r" % (arg1,arg2)) 9 10 #this just takes one argument 11 def print_one(arg1): 12 print("arg1: %r" % arg1) 13 14 #this takes no arguments 15 def print_none(): 16 print("I got nothin'.") 17 18 19 print_two("Zed","Shaw") 20 print_two_again("MI","YO") 21 print_one("only one") 22 print_none() 23 24 def print_three(a,b,c): 25 print("%s %r %r" % (a, b, c)) 26 print_three("a",'b','c')
arg1: 'Zed', arg2: 'Shaw'
arg1: 'MI', arg2: 'YO'
arg1: 'only one'
I got nothin'.
a 'b' 'c'
1. 首先我们告诉 Python 创建一个函数,我们使用到的命令是 def ,也就是“定义
(define)”的意思。
2. 紧接着 def 的是函数的名称。本例中它的名称是 “print_two”,但名字可以随便取,
就叫 “peanuts” 也没关系。但最好函数的名称能够体现出函数的功能来。
3. 然后我们告诉函数我们需要 *args (asterisk args),这和脚本的 argv 非常相似,
参数必须放在圆括号 () 中才能正常工作。
4. 接着我们用冒号 : 结束本行,然后开始下一行缩进。
5. 冒号以下,使用 4 个空格缩进的行都是属于 print_two 这个函数的内容。 其中
第一行的作用是将参数解包,这和脚本参数解包的原理差不多。
6. 为了演示它的工作原理,我们把解包后的每个参数都打印出来,这和我们在之前脚
本练习中所作的类似。
加分题:
1. 函数定义是以 def 开始的吗? yes
2. 函数名称是以字符和下划线 _ 组成的吗? yes
3. 函数名称是不是紧跟着括号 ( ? yes
4. 括号里是否包含参数?多个参数是否以逗号隔开? yes
5. 参数名称是否有重复?(不能使用重复的参数名) no
6. 紧跟着参数的是不是括号和冒号 ): ? yes
7. 紧跟着函数定义的代码是否使用了 4 个空格的缩进 (indent)? yes
8. 函数结束的位置是否取消了缩进 (“dedent”)? yes