在python中实现if语句后如何继续

时间:2018-12-11 21:28:17

标签: python

当程序实现任何if语句时,程序将停止。实现if语句后如何保持其运行?

def main():
    print("Hello i'm your new virtual messenger:")
    inside = input();
    if inside == "Hello" or "Hi" or "Hey" or "Yo":
        print("hello !")
    if inside == "Whats's your name":
        print("My name is Raito")
    if inside == "Who programmed you" or "Who made you" or "Who've made you":
        print("it's you LOL, because i think no one will use this")
    if inside =="What can you do":
        print("Right now nothing special, died waiting to be updated")
    else:
        print("I Don't know how to answer your question, i told you, im waiting to be updated")

if __name__=='__main__':
    main()

1 个答案:

答案 0 :(得分:0)

可能您正在寻找while循环,所以您需要这样的东西:

def main():
    print("Hello I'm your new virtual messenger:")
    while True:
        inside = input();
        if inside in ["Hello","Hi","Hey","Yo"]:
            print("hello !")
        elif inside == "Whats's your name":
            print("My name is Raito")
        elif inside in ["Who programmed you","Who made you","Who've made you"]:
            print("it's you LOL, because I think no one will use this")
        elif inside == "What can you do":
            print("Right now nothing special, died waiting to be updated")
        else:
            print("I Don't know how to answer your question, I told you, I'm waiting to be updated")

if __name__ == '__main__':
    main()

使用此代码,您将永远循环。 如果要在某个时候退出循环,例如,如果从输入中读取“ Exit”,则可以使用关键字break(使您可以在while循环之后继续执行代码),如下所示:

elif inside == "Exit":
    break 

这行代码应与“ if”和“ else”之间的其他“ elif”一样。