summary refs log tree commit diff stats
path: root/python/code/2/5.py
diff options
context:
space:
mode:
Diffstat (limited to 'python/code/2/5.py')
-rw-r--r--python/code/2/5.py35
1 files changed, 35 insertions, 0 deletions
diff --git a/python/code/2/5.py b/python/code/2/5.py
new file mode 100644
index 0000000..ba78eec
--- /dev/null
+++ b/python/code/2/5.py
@@ -0,0 +1,35 @@
+import math, sys
+start_day = int(input('Enter the starting day (1 for Sunday, 2 for Monday, ..., 7 for Saturday): '))
+if not (1 <= start_day <= 7):
+    print('Invalid day, must be between 1 and 7, inclusive')
+    sys.exit(1)
+days_in_month = int(input('Enter the number of days in the month: '))
+print('Calender for this month:')
+print(' SUN MON TUE WED THU FRI SAT')
+if start_day == 2:
+    print(' ' * 4, end='')
+else:
+    print(' ' * (4 * (start_day - 1)), end='')
+
+day = 1
+if start_day != 1:
+    for i in range(7 - start_day + 1):
+        print(f' {day:3}', end='')
+        day += 1
+    print()
+    weeks = math.ceil((days_in_month - (7 - start_day + 1)) / 7)
+else:
+    weeks = math.ceil(days_in_month / 7)
+
+class StopIt(Exception): pass
+
+try:
+    for w in range(weeks):
+        for i in range(7):
+            if day > days_in_month:
+                raise StopIt()
+            print(f' {day:3}', end='')
+            day += 1
+        print()
+except StopIt: pass
+print()