笨办法学python 习题14 提示和传递
习题
习题代码,还是没能把行号复制上去:
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)
likes = input(prompt)
print("where do you live %s?" %user_name)
lives = input(prompt)
print("what kind of computer do you have?")
computer = 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))
运行结果:
PS E:\4.学习案例\习题集\shixi> python ex14.py apple
Hi apple, I'm the ex14.py script.
I'd like to ask you a few questions.
Do you like me ,apple ?
>yes
where do you live apple?
>china
what kind of computer do you have?
>bijiben
alright,so you said 'yes' about liking me.
you live in 'china'. not sure where that is.
and you have a 'bijiben' computer. nice.
将用户提示符设置为变量prompt,这样不需要每次提示用户输入时重复使用提示符了,如果需要修改提示符,也只需要修改一个字符串。
加分题
1.玩游戏
2.将prompt改为其他提示符,重新运行
3.给脚本再添加一个参数,使脚本运行多个参数
将prompt提示符改成>> ,执行结果:
PS E:\4.学习案例\习题集\shixi> python ex14.py apple
Hi apple, I'm the ex14.py script.
I'd like to ask you a few questions.
Do you like me ,apple ?
>>no
where do you live apple?
>>china
what kind of computer do you have?
>>a black computer
alright,so you said 'no' about liking me.
you live in 'china'. not sure where that is.
and you have a 'a black computer' computer. nice
将prompt提示符改成* ,执行结果:
PS E:\4.学习案例\习题集\shixi> python ex14.py apple
Hi apple, I'm the ex14.py script.
I'd like to ask you a few questions.
Do you like me ,apple ?
*no
where do you live apple?
*china
what kind of computer do you have?
*huashuo
alright,so you said 'no' about liking me.
you live in 'china'. not sure where that is.
and you have a 'huashuo' computer. nice.
给脚本再添加一个参数
from sys import argv
script,user_name,report = argv
prompt = '>'
print("Hi %s, I'm the %s script." %(user_name,script))
print("the chinese %s like to ask you a few questions." %report)
print("Do you like me ,%s ?" %user_name)
likes = input(prompt)
print("where do you live %s?" %user_name)
lives = input(prompt)
print("what kind of computer do you have?")
computer = 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))
print("%s said 'I'd like to ask a question.'" %user_name)
运行结果:
PS E:\4.学习案例\习题集\shixi> python ex14.py apple reporter
Hi apple, I'm the ex14.py script.
the chinese reporter like to ask you a few questions.
Do you like me ,apple ?
>no
where do you live apple?
>china
what kind of computer do you have?
>huashuo
alright,so you said 'no' about liking me.
you live in 'china'. not sure where that is.
and you have a 'huashuo' computer. nice.
apple said 'I'd like to ask a question.'