Print month using the month and day

2024/10/14 15:23:05

I need to print month using the month and day. But I cannot seem to move the numbers after '1' to the next line using Python.

# This program shows example of "November" as month and "Sunday"  as day.month = input("Enter the month('January', ...,'December'): ")
day = input("Enter the start day ('Monday', ..., 'Sunday'): ")
n = 1if month == "January" or month == "March" or month == "May" or month == "July" or month == "August" or month == "October" or month == "December":x = 31
elif month == "February":x = 28
else:x = 30print(month)
print("Mo Tu We Th Fr Sa Su")
if (day == "Sunday"):print("                  ", end='')
for i in range (1, 7):for j in range (1, 8):while n != x+1:print('%2s' % n, end=' ')n = n + 1breakprint()

Output looks like this:

November
Mo Tu We Th Fr Sa Su1  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 
Answer

Some changes.

Instead of having a nested loop, just have a single loop that prints all the dates. Then, inside that loop, make the decision about whether to end the line (if the date you just printed corresponded to a Sunday).

Also, the # of days in month look-up is a bit cleaner, and you now handle more "days" than just Sunday:

day = "Monday"
month = "March"# Get the number of days in the months
if month in ["January", "March", "May", "July", "August", "October", "December"]:x = 31
elif month in ["February"]:x = 28
else:x = 30# Get the number of "blank spaces" we need to skip for the first week, and when to break
DAY_OFF = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday']
off = DAY_OFF.index(day)print(month)
print("Mo Tu We Th Fr Sa Su")
# Print empty "cells" when the first day starts after Monday
for i in range(off):print("  ", end=' ')
# Print days of the month
for i in range(x):print("%2d" % (i+1), end=' ')# If we just printed the last day of the week, print a newlineif (i + off) % 7 == 6: print()

March/Monday

March
Mo Tu We Th Fr Sa Su1  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 

March/Sunday

March
Mo Tu We Th Fr Sa Su1 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 

February/Sunday

February
Mo Tu We Th Fr Sa Su1 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 
https://en.xdnf.cn/q/117939.html

Related Q&A

Maya: Defer a script until after VRay is registered?

Im trying to delay a part of my pipeline tool (which runs during the startup of Maya) to run after VRay has been registered. Im currently delaying the initialization of the tool in a userSetup.py like…

Optimization on Python list comprehension

[getattr(x, contact_field_map[communication_type])for x in curr_role_group.contacts ifgetattr(x, contact_field_map[communication_type])]The above is my list comprehension. The initial function and the …

tensorflow Word2Vec error

I downloaded source code of word2vec in github below. https://github.com/tensorflow/models/blob/master/tutorials/embedding/word2vec.py I am using tensorflow on pycharm. Im using windows 10. I installed…

Mysterious characters at the end of E-Mail, received with socket in python

I am not sure if this is the right forum to ask, but I give it a try. A device is sending an E-Mail to my code in which I am trying to receive the email via a socket in python, and to decode the E-Mail…

Error when importingPython-vlc

I have installed python-vlc D:\Programing\Python\Python 3.6>python -m pip install python-vlc Requirement already satisfied: python-vlc in d:\programing\python\python 3.6\lib\site-packages\python_vlc…

How to use FEniCS in Jupyter Notebook or Spyder?

I have recently installed FEniCS using Docker with following commandTerminal> curl -s https://get.fenicsproject.org | bashEvery thing works well when I am in fenicsproject session. In this session w…

Error installing polyglot in python 3.5.2

I want to do sentiment analysis on urdu sentences. I searched a python package Polyglot having URDU POS tagger in it. But on installing, it prompts error;Any way out?

How do you turn a dict of lists into a list of dicts with all combinations?

Basically what I am looking for is the equivalent of itertools.product but for dicts.For example, say I want to test a function for all combinations of its keyword arguments, I would like to pass lists…

Django differentiate between the first time user and returning user

I am using django registration redux for login and auth purposes. I want to do the following.if the user logs in for the first time i want to redirect to URL-"profile/create" if the user is a…

Automatically cropping an image using Python

I want to automatically crop an image using OpenCV into many images, the number of output images is variable. I started by replacing the white background by a transparent background.The input image: I …