有没有一种方法可以为python的cmd模块中的函数添加值(在使用程序时)

2024-10-01 09:31:54 发布

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

我对python中的CMD模块有问题。我需要能够在do语句之后添加值,例如:

def do_test(self,x,y):
    calculate = x * y
    print (calculate)

我试着阅读python网站上关于cmd.py的原始文档,但作为初学者,我很难理解其中的任何一个

我也试着在github、stackoverflow、reddit和youtube上寻找和我想做的一样的项目。但似乎cmd模块更多地用于不获取任何新值的静态函数

import cmd
import sys

class TestCMDcmd.Cmd):
    intro = 'Welcome to this simple command prompt'
    prompt = ">>"

# i want to do something like this using the cmd library
def do_test(self,x,y):
    print (x*y)
# psudo code:
'''
def addnumbers (x,y):
    print (x,y)

command = input("addnumbers (4,2)")
if command == add numbers + # some kind of way of writing for example 2     and 4 :
print (addnumbers(inputvalue1 and input value2))



'''

def do_quit(self,arg):
    """Exit command prompt"""
    return True

if __name__ == '__main__':
    TestCMDcmd().cmdloop()

Tags: 模块totestimportselfcmddefthis