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

2024-05-26 06:21:13 发布

您现在位置:Python中文网/ 问答频道 /正文

我对练习13“艰难地学习Python”的额外学分有困难。
它想让我将argv与原始输入相结合,但我无法理解。
有人能帮我吗?例子太好了!
非常感谢!
编辑:练习的原始代码是:

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

Tags: 代码from编辑yourissysscriptvariable
3条回答
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()

我就是这样做的:

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

举个例子和答案很难区分,这不太可能是帮助你的最佳方法。不过,也许你想得太多了。我相信这个想法是使用一些命令行输入(进入argv)和一些输入输入(通过原始输入)来生成一个脚本来报告这两者。例如,它可能产生:

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

相关问题 更多 >