猿问

使用argparse显示主题

我的目标是通过使用argparse(CLI)向用户提出问题,然后以类似于以下内容的方式将输入存储为字符串:


marker = input("Name of marker: ")

Location = input("Name of location: ")

我不确定如何执行此操作,但这是我目前拥有的:


parser = argparse.ArgumentParser(description = 'Collect Information')

parser.add_argument('marker', help = 'Name of marker')

parser.add_argument('location', help = 'Name of location')

args = parser.parse_args()


函数式编程
浏览 140回答 3
3回答

慕尼黑5688855

您拥有的一切都很好;您只需要知道从何处获取参数即可。parser = argparse.ArgumentParser(description='Collect Information')parser.add_argument('marker', help='Name of marker')parser.add_argument('location', help='Name of location')args = parser.parse_args()print("Your marker is {}".format(args.marker))print("Its location is {}".format(args.location))通常,的返回值为parse_args每个自变量都有一个属性,其名称取自自变量的名称。
随时随地看视频慕课网APP

相关分类

Python
我要回答