-3

So I am writing a small code and I want to exit a particular loop when the user presses 'ENTER' key. I am a beginner so please help me with this.

Code:

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)
TheLazyScripter
  • 2,541
  • 1
  • 10
  • 19

1 Answers1

2

Try checking the input against the empty string:

while True:
    text = raw_input("Prompt (or press Enter to Exit): ")
    if text == "":
        break
    # Code if the user inputted something
DoubleMx2
  • 375
  • 1
  • 9
  • 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) main() – Akshay Bhaskaran Jun 01 '16 at 05:10
  • That code should be added to the actual question, I can't see any formatting (indentation and line breaks) in the comments. But I think the main problem is that it appears you call main() at the end of the leapYear function, but the while loop in main is what is causing the program to run until enter is hit, try removing main() in the leapYear function. – DoubleMx2 Jun 01 '16 at 05:15
  • @AkshayBhaskaran I edited the OP with the code above. Please check to ensure formatting is correct. – TheLazyScripter Jun 01 '16 at 05:20