简体   繁体   English

询问用户输入,运行指定功能,然后返回询问

[英]Ask for user input, run specified function, then go back to asking

I am making a CLI in python using click. 我正在使用click在python中创建CLI。 What i want to have is a kind of "idle state" which the program starts in. The script asks for user input, and depending on the input a specific function should be run. 我想拥有的是一种在程序开始时处于“空闲状态”的状态。脚本要求用户输入,并应根据输入运行特定的功能。 This sub-function can also ask for user input and do different things. 该子功能还可以要求用户输入并执行其他操作。

What im having trouble with is making the program return to the "idle state" after the function has completed running. 我遇到的麻烦是在函数完成运行后使程序返回“空闲状态”。

@click.command()    
def cli():
     print "Welcome. What do you want to do?"
     choice = click.prompt('')
     if choice == 'add':
            add()
     else:
            subtract()

So after running eg add() i want to program to once again print "Welcome" and prompt the user for input. 因此,在运行例如add()之后,我想编程以再次打印“ Welcome”并提示用户输入。 Wrapping the cli()-function in a while-loop didn't work - im suspecting click breaks it somehow. 在一个while循环中包装cli()函数不起作用-我怀疑单击会以某种方式破坏它。

you can try this: 您可以尝试以下方法:

def cli():
    print "Welcome. What do you want to do?"
    choice = click.prompt('')
    if choice == 'add':
        add()
        cli()
    else:
        subtract()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM