Python在ENTER键上退出循环

时间:2016-06-01 04:49:27

标签: python loops

所以我正在编写一个小代码,当用户按下“输入”时,我想退出一个特定的循环。键。我是初学者所以请帮助我。

代码:

def leapYear(year):
    if year%4==0:
        print("\nThe year", year, "is a leap year!") 
    else: 
        print("\nThe year", year, "is NOT a leap year!")
        print('')
        main()
def main(): 
    while True: 
        year = int(input("Please enter a 4-digit year \n[or 'ENTER' to quit]: ")) 
        if year == "": 
            break 
            leapYear(year)

1 个答案:

答案 0 :(得分:1)

尝试针对空字符串检查输入:

while True:
    text = raw_input("Prompt (or press Enter to Exit): ")
    if text == "":
        break
    # Code if the user inputted something