习题 14: 提示和传递
参考上一习题的做法!
1.新建一个文件,文件的名称为temp.py
2.将下面的代码复制到文件中
from sys import argv
script,user_name = argv
prompt = '> '
print("Hi %s,I'm the %s script."%(user_name,script))
print("I'd like to ask you a few questions.")
print("Do you like me %s?" % user_name)
likes = input(prompt)
print("Where do you live %s?" % user_name)
lives = input(prompt)
print("What kind of computer do you have?")
computer = input(prompt)
print("""
\t*Alright,so you said %r about liking me.
\t*You livie in %r. Not sure where that is.
\t*And you have a %r computer. Nice.""" % (likes,lives,computer))
3.找到temp.py的文件,在该文件夹下空白处按住 Shift+鼠标右键,找到在此处打开Powershell窗口
4.在窗口内输入 python .\temp.py Zed 显示结果如下:
PS C:\Users\abcd\.spyder-py3> python .\temp.py Zed
Hi Zed,I'm the .\temp.py script.
I'd like to ask you a few questions.
Do you like me Zed?
> Yes
Where do you live Zed?
> San Francisco
What kind of computer do you have?
> Tandy
*Alright,so you said 'Yes' about liking me.
*You livie in 'San Francisco'. Not sure where that is.
*And you have a 'Tandy' computer. Nice.
PS C:\Users\abcd\.spyder-py3>
加分习题
- 查一下 Zork 和 Adventure 是两个怎样的游戏。 看看能不能下载到一版,然后玩玩看。
- 将 prompt 变量改成完全不同的内容再运行一遍。
- 给你的脚本再添加一个参数,让你的程序用到这个参数。
- 确认你弄懂了三个引号 “”" 可以定义多行字符串,而 % 是字符串的格式化工具。