from sys import argv
script, user_name = argv
# 提示符
prompt = '>'
print(f"Hi {user_name}, I'm the {script} script.")
print("I'd like to ask you a few questions.")
print(f"Do you like me {user_name}?")
likes = input(prompt)
print(f"Where do you live {user_name}?")
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}. Nice.
""")
练习
- Find out what the games Zork and Adventure were. Try to find a copy and play it.
- Change the prompt variable to something else entirely.
- Add another argument and use it in your script, the same way you did in the previous exercise withfirst, second = ARGV .
- Make sure you understand how I combined a """ style multiline string with the {} format activatoras the last print.
答案
from sys import argv
script, name = argv
dog = ">"
print("Hello {name}, I'm {script}.")
print(f"I'd like to ask some questions.")
print(f"Do you like {name}?")
likes = input(dog)
print(f"where do you live {name}")
home = input(dog)
print("What kind of computer do you have?")
computer_band = input(dog)
print(f"""
Alright, so you said {likes} about liking me.
You live in {home}. Not sure where that is.
And you have a {computer_band}. Nice
""")