Exit while loop by user hitting ENTER key

前端 未结 14 1888
难免孤独
难免孤独 2020-12-08 23:01

I am a python newbie and have been asked to carry out some exercises using while and for loops. I have been asked to make a program loop until exit is requested by the user

14条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-09 00:05

    I ran into this page while (no pun) looking for something else. Here is what I use:

    while True:
        i = input("Enter text (or Enter to quit): ")
        if not i:
            break
        print("Your input:", i)
    print("While loop has exited")
    

提交回复
热议问题