小编Joh*_*ohn的帖子

在命令行中运行 python 脚本不会打印任何输出

我想用 python 编写一个脚本,然后从命令行运行它。调用脚本test.py并运行它的命令是:

python3 test.py John Jackson
Run Code Online (Sandbox Code Playgroud)

这是预期的输出:

John Jackson
Run Code Online (Sandbox Code Playgroud)

这是我制作的 python 脚本:

class person:

    def __init__(self, first, last):
        self.firstname = first
        self.lastname = last

    def get_first(self):
        return self.firstname

    def get_last(self):
        return self.lastname

import argparse

def main():
    parser = argparse.ArgumentParser()
    parser.add_argument('first')
    parser.add_argument('last')
    args = parser.parse_args()
    c1 = person(args.first, args.last)
    print(c1)
Run Code Online (Sandbox Code Playgroud)

但问题是,当我使用上述命令从命令行运行脚本时,它什么也不返回。您知道如何修复它并获得预期的输出吗?

python command-line argparse python-3.x

5
推荐指数
2
解决办法
2万
查看次数

标签 统计

argparse ×1

command-line ×1

python ×1

python-3.x ×1