Python 是/否 定义

1 投票
2 回答
7608 浏览
提问于 2025-04-16 11:07

我正在使用下面的例子:

像APT命令行那样的是/否输入?

我想把它定义成一个自己的功能,然后在需要的时候调用它,像这样:

def log_manager():  
    question = "Do you wish to continue?"  
    choice = query_yes_no_quit(question, default="yes")  
        if choice == 'y':  
            print ("you entered y")  
        else:     
            print ("not working")  

无论我输入什么,"not working"总是会被打印出来。任何指导都会非常感谢!

2 个回答

1

使用:

if choice:
    print("you entered y")
else:
    print("not working")

这个函数会返回 TrueFalse,而不是 "y""n"

9

这个函数会返回真或假。所以你可以用 if choice: 来判断。

顺便说一下,其实你可以通过加上 print choice 来轻松找到答案哦;)

撰写回答