Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python解析到一个游戏_Python_Python 3.x_Argparse - Fatal编程技术网

Python解析到一个游戏

Python解析到一个游戏,python,python-3.x,argparse,Python,Python 3.x,Argparse,我在试着找出我的冒险游戏的argparse。 这是我的代码: parser = argparse.ArgumentParser(description='Adventure') parser.add_argument('--cheat','-c', action='store_true', help='cheat') parser.add_argument('--info','-i', action='store_true', help='Information' ) parser.add_ar

我在试着找出我的冒险游戏的argparse。 这是我的代码:

parser = argparse.ArgumentParser(description='Adventure')
parser.add_argument('--cheat','-c', action='store_true', help='cheat')
parser.add_argument('--info','-i', action='store_true', help='Information' )
parser.add_argument('--version','-v', action='store_true', help='Version' )
parser.add_argument('--about', '-a', action='store_true', help='About' )

args = parser.parse_args()

if args.cheat or args.c:
    print("Cheat")
    sys.exit()
elif args.info or args.i:
    print("Information")
    sys.exit()
elif args.version or args.v:
    print("Version")
    sys.exit()
elif args.about or args.a:
    print("About")
    sys.exit()
else:
    #Game code#
但当我有这些论点时,我就犯了一个错误。当我只开始作弊时,它工作得很好,但当我把所有其他的加起来时,它就搞砸了。要么我真的不知道我在做什么,要么我看不出有什么不对

这是我的错误:

$ python3 adventure.py --info
  Traceback (most recent call last):
  File "adventure.py", line 12, in <module>
  if args.cheat or args.c:
      AttributeError: 'Namespace' object has no attribute 'c'
$python3 advention.py--info
回溯(最近一次呼叫最后一次):
文件“adventure.py”,第12行,在
如果args.cheat或args.c:
AttributeError:“命名空间”对象没有属性“c”
我之所以这样做是因为我想在终端中输入这个而不启动游戏,所以如果你只需输入python3 advention.py,游戏就会启动。但我现在甚至不能这样做:P。 只有python3 adventure.py-c和python3 adventure-cheat有效

有人知道这个问题的解决办法吗


谢谢。

错误消息非常清楚:
'Namespace'对象没有属性“c”

argparse
library自动确定存储命令行选项值的属性名称:

对于可选参数操作,
dest
的值通常从选项字符串推断出来
ArgumentParser
通过获取第一个长选项字符串并去除初始的
--
字符串来生成
dest
的值。如果未提供长选项字符串,
dest
将从第一个短选项字符串派生,方法是剥离初始的
-
字符

由于选项有长名称和短名称,因此使用长名称。换句话说,将
if args.cheat或args.c:
替换为
if args.cheat: