学习Python艰难的帮助:练习13

时间:2012-02-18 02:21:27

标签: python

我在学习Python艰难之路的练习13中获得了额外的功劳 它希望我将argv与raw_input结合起来,这是我无法弄清楚的 任何人都可以帮我吗?例子很棒!
非常感谢!
编辑:练习的原始代码是:

from sys import argv

script, first, second, third = argv

print "The script is called:", script
print "Your first variable is:", first
print "Your second variable is:", second
print "Your third variable is:", third

5 个答案:

答案 0 :(得分:2)

一个例子与答案无法区分,这不太可能是帮助你的最佳方式。也许你正在思考这个问题。我相信这个想法是使用一些命令行输入(进入argv)和一些输入输入(通过raw_input)来制作一个报告两者的脚本。例如,它可能会产生:

The script is called: ex13.py
Your first variable is: cheese
Your second variable is: apples
You entered the following data: foo bar baz

答案 1 :(得分:1)

这就是我尝试这样做的方式:

from sys import argv

script, weather, feeling = argv

print "Hot or Cold",
weather = raw_input()

print "Happy or sad",
feeling = raw_input()

print "The name of the script is:" , script
print "The day is:", weather
print "Today I am feeling:", feeling

答案 2 :(得分:0)

import sys

def main():
    all_args = sys.argv[:]
    user = None
    while user != 'STOP':
        user = raw_input('You have %d args stored. Enter STOP or add another: ' % len(all_args))
        if user != 'STOP':
            all_args.append(user)
    print 'You entered %d args at the command line + %d args through raw_input: [%s]' % (len(sys.argv), len(all_args) - len(sys.argv), ', '.join(all_args))

if __name__ == '__main__':
    main()

答案 3 :(得分:0)

这对我来说也很困惑。作者把它放在底层的常见问题中。

问:我无法将argv与raw_input()结合使用。 答:不要过分思考它。只需在此脚本末尾打两行,使用raw_input()获取内容然后将其打印出来。从那开始玩更多的方法在同一个脚本中使用它们。

答案 4 :(得分:0)

以下是我解决这个问题的方法:(注意:最初运行脚本时仍需要提供参数)

from sys import argv

script, first, second, third = (argv)

print "The script is called:", script
print "Your first variable is:", first
print "Your second variable is:", second
print "Your third variable is:", third

first = raw_input("\nNew First Variable? ")
second = raw_input("New Second Variable? ")
third = raw_input("New Last Variable? ")

print "\n\nYour new variables are %s, %s, and %s" % (first, second, third)

这是我得到的输出:

C:\Users\mbowyer\Documents\Python_Work>python ex13a.py 1 2 3


The script is called: ex13a.py

Your first variable is: 1

Your second variable is: 2

Your third variable is: 3

New First Variable? a

New Second Variable? b

New Last Variable? c

Your new variables are a, b, and c

C:\Users\mbowyer\Documents\Python_Work>