学习目标:
使用argv()和input()
使用input(),利用>作为提示符
了解Zork和Adventure两款游戏
代码如下:
from sys import argv
script,Marry = argv,'Marry'
prompt = '>'
print(f"Hi {Marry},I'm the {script} script.")
print("I'd like to ask you a few questions.")
print(f"Do you like me ? {Marry}")
likes = input(prompt)
print(f"Where do you live? {Marry}")
lives = input(prompt)
print("What kind of computer do you have?")
computer = input(prompt)
print(f"""
Alright,so you said {likes} about liking me.
You live in {lives}. Not sure where that is.
And you have a {computer} computer. Nice.
""")
结果输出
Hi Marry,I'm the ['D:/Bella/ex14.py'] script.
I'd like to ask you a few questions.
Do you like me ? Marry
>Yes
Where do you live? Marry
>San Francisco
What kind of computer do you have?
>Tandy 1000
巩固练习
1. 将prompt变量改成完全不同的内容再运行一次
from sys import argv
script,Marry = argv,'Marry'
aa = '>'
print(f"Hi {Marry},I'm the {script} script.")
print("I'd like to ask you a few questions.")
print(f"Do you like me ? {Marry}")
likes = input(aa)
print(f"Where do you live? {Marry}")
lives = input(aa)
print("What kind of computer do you have?")
computer = input(aa)
print(f"""
Alright,so you said {likes} about liking me.
You live in {lives}. Not sure where that is.
And you have a {computer} computer. Nice.
""")
######结果输出
Hi Marry,I'm the ['D:/Bella/ex14.py'] script.
I'd like to ask you a few questions.
Do you like me ? Marry
>Yes
Where do you live? Marry
>San Francisco
What kind of computer do you have?
>Tandy 1000
Alright,so you said Yes about liking me.
You live in San Francisco. Not sure where that is.
And you have a Tandy 1000 computer. Nice.
2. 再加一个参数。
from sys import argv
script,Marry,Jonn = argv,'Marry','Jonn'
aa = '>'
bb = '>>--'
print(f"Hi {Marry} and {Jonn},I'm the {script} script.")
print("I'd like to ask you a few questions.")
print(f"Do you like me ? {Marry}")
likes = input(aa)
print(f"So do you? {Jonn}")
likes1 = input(bb)
print(f"Where do you live? {Marry}")
lives = input(aa)
print(f"So do you? {Jonn}")
lives1 = input(bb)
print("What kind of computer do you have?")
computer = input(aa)
print(f"So do you? {Jonn}")
computer1 = input(bb)
print(f"""
Alright,so Marry said {likes} about liking me, and Jonn also said {likes} about liking me.
You live in {lives}, and Jonn is live in {lives1}.
And you have a {computer} computer, and Jonn has a {computer1}.Really Nice to me.
""")
结果输出:
Hi Marry and Jonn,I'm the ['D:/Bella/ex14.py'] script.
I'd like to ask you a few questions.
Do you like me ? Marry
>Yes
So do you? Jonn
>>--No
Where do you live? Marry
>Pineapple house
So do you? Jonn
>>--Stone house
What kind of computer do you have?
>HP
So do you? Jonn
>>--Mac
Alright,so Marry said Yes about liking me, and Jonn also said Yes about liking me.
You live in Pineapple house, and Jonn is live in Stone house.
And you have a HP computer, and Jonn has a Mac.Really Nice to me.
进程已结束,退出代码0
Tips:
- 出现报错内容:
syntaxError:invalid syntax
#python环境问题
ValueError: not enough values to unpack
#命名的参数不够
NameError:name 'prompt' is not defined
#拼错了prompt,要么漏掉写了这一行,要么没有创建这个变量
print(f'''
TypeError: 'tuple' object is not callable
#tuple对象是不可调用的
#“[ ]” 和“()”是否用错了,是否误把数据类型当做函数调用
print(f'''
TypeError: 'str' object is not callable
#检查字符串后面加了括号调用
#这行代码上面print()是否正确调用
这篇博客介绍了如何在Python中使用`sys.argv`获取命令行参数,并结合`input()`函数实现用户交互。通过两个巩固练习展示了如何改变提示符`prompt`以及添加额外参数,从而实现不同场景下的用户对话流程。例子中涉及到了字符串格式化输出以及简单的用户输入处理。
1328

被折叠的 条评论
为什么被折叠?



