习题14:提示和传递
练习代码
# coding: utf-8
__author__ = 'www.py3study.com'
from sys import argv
script, username = argv
prompt = '>'
print("Hi {}, I'm the {} script.".format(username, script))
print("I'd like to ask you a few questions.")
print("Do you line me {} ?".format(username))
likes = input(prompt)
print("Where do you live {} ?".format(username))
lives = input(prompt)
print("What kind of computer do you have ?")
computer = input(prompt)
print("""Alright, so you said {} about liking me.
You live in {}. Not sure where that is.
And you have a {} computer. Nice.
""".format(likes, lives, computer))
将用户提示符设置为变量prompt,这样就不需要在每次用到input()时重复输入提示用户的提示符了,如果需要修改提示符,也只是修改一个位置而已。
应该看到的结果,记住当运行脚本时,需要把你的名字赋值给这个脚本,让argv参数接收到你的名称
E:\test>python lianxi_14.py Sam
Hi Sam, I'm the lianxi_14.py script.
I'd like to ask you a few questions.
Do you like me Sam ?
>yes
Where do you live Sam ?
>China
What kind of computer do you have ?
>Shanghai
Alright, so you said yes about liking me.
You live in China. Not sure where that is.
And you have a Shanghai computer. Nice.
常见问题
运行时出现SyntaxError: invalid syntax
再次说明,你应该使用命令行,而不是python环境去运行脚本
免责声明:
① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。
② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341