如何使用户退出while循环

阿尔弗雷德·H。

我正在尝试一会儿循环,但后来我去了用户输入5退出循环。我在python 3中

def main():
    print("Welcome to the List Info Checker")
    printMenu()
    printValue = input("Please enter a number between 1 and 5(inclusive): ")
    while printValue != 5:
        if printValue == 1:
            print("1")
        elif printValue == 2:
           # allTheSame()
            print("2")
        elif printValue == 3:
           # allDifferent()
            print("3")
        elif printValue == 4:
           # sortThis()
            print("4")

main()
ud

有2种方法可以打破while循环

  1. 使语句以某种方式更改,以使其不再存在 True

  2. 使用break命令突破“最低级别的循环”

因为仅在循环之前询问用户输入,所以在循环开始导致无穷循环之后,它不可能更改。如果输入在循环内,则用户可以输入5来中断输入,因为每次循环重新启动时都会询问:

printValue = input("Please enter a number between 1 and 5(inclusive): ")

while printValue != 5:

    if printValue == 1:
        print("1")
    elif printValue == 2:
       # allTheSame()
        print("2")
    elif printValue == 3:
       # allDifferent()
        print("3")
    elif printValue == 4:
       # sortThis()
        print("4")

    printValue = input("Please enter a number between 1 and 5(inclusive): ")

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章