2021-09-11

本文介绍如何在Python中通过变量prompt实现提示符的灵活性,并演示如何接收并处理多个命令行参数,提升交互体验。通过实例展示了如何将提示符改为‘>’, ‘*’等,同时增加了新的参数用于扩展脚本功能。
摘要由CSDN通过智能技术生成

笨办法学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.'
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值