Ex14 Prompting and Passing

argvraw_input 联合使用

在 Ex14,将综合使用 argvraw_input。代码里用到提示符,以指示用户输入信息。

示例代码 Ex14:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
from sys import argv
script, user_name = argv
prompt = '> '
print "Hi %s, I'm the %s script." % (user_name, script)
print "I'd like to ask you a few questions."
print "Do you like me %s?" % user_name
like = raw_input(prompt)
print "Where do you live %s?" % user_name
lives = raw_input(prompt)
print "What kind of computer do you have?"
computer = raw_input(prompt)
print """
Alright, so you said %r about liking me.
You live in %r. Not sure where that is.
And you have a %r computer. Nice.
""" % (likes, lives, computer)

这里不贴运行结果了,把出错信息贴出来:

小结

  1. 末尾,输入字符块,要在英文输入法下录入双引号,否则会提示错误。
  2. 变量较多时, 要注意名称一致,不要有拼写错误,导致程序识别不出。
Licensed under CC BY-NC-SA 4.0
使用 Hugo 构建
主题 StackJimmy 设计