学习目标:
使用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()是否正确调用