1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#!/usr/bin/env python
# -*- coding: utf8 -*-

import sys, datetime

# Function performing the tests.
def is_leap(year):
    if not year%4:
        if not year%100:
            if not year%400:
                return 'Das Jahr %04d ist ein Schaltjahr' % year
            return'Das Jahr %04d ist kein Schaltjahr' % year
        return 'Das Jahr %04d ist ein Schaltjahr' % year
    else:
        return' Das Jahr %04d ist kein Schaltjahr' % year

print 'Ist ein Jahr ein Schaltjahr?'
print 'Beenden mit Eingabe "quit"'
while True:
    # This loop truely runs forever. The script will never leave it
    # until the script is terminated using sys.exit().
    while True:
        # This loop is repeated until a valid integer is entered.
        try:
            year = raw_input('Jahr?> ')
            year = int(year)
        except EOFError:
            # This happens when Ctrl+D is hit.
            print "Bye!"
            sys.exit(0)
        except KeyboardInterrupt:
            # This happens when Ctrl+C is hit.
            print "Bye!"
            sys.exit(0)
        except:
            if year == "quit":
                print "Bye!"
                sys.exit(0)
            else:
                print "Kann nicht in Integer umgewandelt werden:", year
        else:
            break
    print is_leap(year)