Python 官方文档:入门教程 => 点击学习
练习代码# coding: utf-8 __author__ = 'www.py3study.com' from sys import argv scri
练习代码
# 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环境去运行脚本
--结束END--
本文标题: 习题14:提示和传递
本文链接: https://lsjlt.com/news/178859.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-03-01
2024-03-01
2024-03-01
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0