欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

笨办法13参数、解包、变量_草稿

程序员文章站 2022-06-15 18:45:08
...

加分习题3:将 raw_input 和 argv 一起使用,让你的脚本从用户手上得到更多的输入。

from sys import argv

script, first, second, third = argv #试了一下,这里的第一个参数script可以随便改成啥,不影响运行结果
'''
print "The script is called:", script
print "Your first variable is:", first
print "Your second variable is:", second
print "Your third variable is:", third
'''

raw_input("The script is called: "), script
raw_input("Your first variable is: "), first
raw_input("Your second variable is: "), second
raw_input("Your third variable is: "), third

不知道是不是这个意思,总觉得怪怪的,先这样后面再改!

运行结果如下,需要手动输入变量名:
笨办法13参数、解包、变量_草稿

【下班啦下班啦!!

==============下面重写!==============

from sys import argv

script, first, second, third = argv
prompt = ">"

print "please enter the script name:"
script = raw_input(prompt)

print "please enter the first variable:"
first = raw_input(prompt)

print "please enter the second name:"
second = raw_input(prompt)

print "please enter the third name:"
third = raw_input(prompt)

print """
The script is called:%r
The first variable is:%r
The second variable is:%r 
The third variable is:%r
""" % (script, first, second, third)

运行结果:
笨办法13参数、解包、变量_草稿

ps:但是怎么才能把引号去掉呢。。 埃玛以后再说吧!
来解答! 重新翻看了之前的第6课,只要把%r换成%s就没有引号啦!!


20170914 按照22课练习来复习

argv 和 raw_input()的区别:要求用户输入的位置不同。在命令行输入参数用argv.,在脚本执行的过程中输入参数用raw_input()