练习14--提示和传递

一 主要语法

本次习题的主要语法内容是:用一种不同的方式使用input ,就是让它打印出一个简单的 > 提示符。

  • 具体做法:定义一个提示符变量:prompt = ' > '
  •                  在某个变量需要输入的时候,调用input函数的方式为:likes = input(prompt)
  •       注意:我们把用户提示符设置成变量 prompt ,然后把它赋给 input 而不是一遍遍地输入它们。现在如果我们想把提示符变成别的东西,只需要在定义prompt变量的时候修改引号内的内容即可

二 代码及运行结果

1 代码:

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(f"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.
  """)

2 运行结果

PS E:3_work4_python2_code2_LearnPythonTheHardWay> python ex14.py Zed
Hi Zed,I'm the ex14.py script.
I'd like to ask you a few questions.
Do you like me Zed?
 >>> Yes
Where do you live Zed?
 >>> San Francisico
What kind of computer do you have?
 >>> Tandy 1000

Alright, so you said Yes about liking me.
You live in San Francisico.Not sure where that is.
And you have a Tandy 1000 computer.Nice.

 三 游戏

1 zork游戏:https://www.douban.com/group/topic/21962101/

2 Adcenture:https://baijiahao.baidu.com/s?id=1632958318937965675&wfr=spider&for=pc

原文地址:https://www.cnblogs.com/luoxun/p/13200883.html