习题14:提示和传递
from sys import argv
script, userName = argv, "smallfish"
prompt = '>'
print("Hi %s,I'm the %s script." % (userName, script))
print("I'd like to ask you a few questions.")
print("Do you like me %s?" % userName)
likes = input(prompt)
print("Where do you live %s?" % userName)
lives = input(prompt)
print("What kind of computer do you have?")
computer = input(prompt)
print("""
Alright,so you said %s about liking me.
You live in %s.Not sure where that is.
And you have a %s computer.Nice.
""" % (likes, lives, computer))
1. 查一下 Zork 和 Adventure 是两个怎样的游戏。 看看能不能下载到一版,然后玩玩看。
2. 将 prompt 变量改成完全不同的内容再运行一遍。
3. 给你的脚本再添加一个参数,让你的程序用到这个参数。
4. 确认你弄懂了三个引号 """ 可以定义多行字符串,而 % 是字符串的格式化工具。
习题15:读取文件
from sys import argv
script, filename = argv, "demo8.txt"
txt = open(filename)
print("Here's your file %s:" % filename)
print(txt.read())
print("Type the filename again:")
fileAgain = input("> ")
txtAgain = open(fileAgain)
print(txtAgain.read())
Here's your file demo8.txt:
This is stuff I typed into a file.
It is really cool stuff.
Lots and lots of fun to have in here.
Type the filename again:
> demo8.txt
This is stuff I typed into a file.
It is really cool stuff.
Lots and lots of fun to have in here.
1. 在每一行的上面用注解说明这一行的用途。
2. 如果你不确定答案,就问别人,或者上网搜索。大部分时候,只要搜索 “python” 加上你要搜的东西就能得到你要的答案。比如搜索一下“python open”。
3. 我使用了“命令”这个词,不过实际上它们的名字是“函数(function)”和“方法(method)。上网搜索一下这两者的意义和区别。看不明白也没关系,迷失在别的程序员的知识海洋里是很正常的一件事情。
4. 删掉 10-15 行使用到 raw_input 的部分,再运行一遍脚本。
5. 只是用 raw_input 写这个脚本,想想那种得到文件名称的方法更好,以及为什么。
6. 运行 pydoc file 向下滚动直到看见 read() 命令(函数/方法)。看到很多别的命令了吧,你可以找几条试试看。不需要看那些包含 __ (两个下划线)的命令,这些只是垃圾而已。
7. 再次运行 python 在命令行下使用 open 打开一个文件,这种 open 和 read 的方法也值得你一学。
8. 让你的脚本针对 txt and txt_again 变量执行一下 close() ,处理完文件后你需要将其关闭,这是很重要的一点。
print_r('点个赞吧');
var_dump('点个赞吧');
NSLog(@"点个赞吧!")
System.out.println("点个赞吧!");
console.log("点个赞吧!");
print("点个赞吧!");
printf("点个赞吧!\n");
cout << "点个赞吧!" << endl;
Console.WriteLine("点个赞吧!");
fmt.Println("点个赞吧!")
Response.Write("点个赞吧");
alert(’点个赞吧’)