gpt4 book ai didi

python - 接受 1 个位置参数(给定 2 个)

转载 作者:太空宇宙 更新时间:2023-11-03 18:32:05 24 4
gpt4 key购买 nike

当我在 python 输入框中调用 inform 时,它说我正在提供 do_inform 2 个参数,但我只输入“inform”并单击“确定”。为什么它说我提供了两个参数?

import cmd

class DisplayWelcome(cmd.Cmd):
"""Welcome user to game"""

def do_inform(self):
k = input('Enter a letter')
print (k)


def main():
d = DisplayWelcome()
#d.do_greet()
d.do_inform()
d.cmdloop()

if __name__ == '__main__':
main()

最佳答案

所有 Cmd.do_* 方法都会传递命令行的其余部分,即使是空的。

当您在命令提示符下键入 inform 时,该行的其余部分为空,因此 Cmd 调用 self.do_inform('').

始终为所有 do_* 方法提供剩余部分的参数;如果您愿意,可以忽略它:

def do_inform(self, rest=None):
k = input('Enter a letter')
print (k)

cmd library documentation并没有说得很清楚; cmd.cmdloop() 方法文档的顶部提到了这一点:

Repeatedly issue a prompt, accept input, parse an initial prefix off the received input, and dispatch to action methods, passing them the remainder of the line as argument.

关于python - 接受 1 个位置参数(给定 2 个),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22244837/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com