Python Forum

Full Version: write a program which prints the day in english
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
Hi, I made post before ( dont mind it, it was when i just started ).
I have a question:
how to print the day in english if i have a date ( input date ).
I managed to do this, but it was long and i cant think that there is a solution way easier and shorter than this..
i need to write in english the day.
import calendar
 
year = int(input("Please choose your year: "))
month = int(input("Please choose your month: "))
day = int(input("Please choose your day: "))
 
print(f"Please enter a date: {day}/{month}/{year}")
calendar = calendar.weekday(year, month, day)
print(f"The day of the week is: {calendar}")
 
if calendar == 0:
    print("Day of the week in english is Monday")
    
elif calendar == 1:
    print("Day of the week in english is Tuesday")
    
elif calendar == 2:
    print("Day of the week in english is Wednesday")
    
elif calendar == 3:
    print("Day of the week in english is Thursday")
    
elif calendar == 4:
    print("Day of the week in english is Friday")
    
elif calendar == 5:
    print("Day of the week in english is Saturday")
    
elif calendar == 6:
    print("Day of the week in english is Sunday")
the link shows the code

how to make it shorter + how to import the date in number to date in english?
i answered for it correctly ( as you can check there ), but its long and slappy...[Image: bJf9]
You can use calendar.day_name to get the day name string
import calendar
print(f"Day of the week in english is {calendar.day_name[0]}")
Output:
Day of the week in english is Monday
what do you mean? how do i connect it with my code? i cant seem to understand.

edit: im trying to connect it with my code and it wont work to me...
calendar.weekday returns an int for the weekday, calendar.day_name is indexable using the weekday int
import calendar
for day in (23, 24, 25):
    weekday = calendar.weekday(2021, 7, day)
    print(f"Day of the week in english is {calendar.day_name[weekday]}")
Output:
Day of the week in english is Friday Day of the week in english is Saturday Day of the week in english is Sunday
ohh what i learned so far doesnt include for.
I know what for is, but im learning with a new course and it asks not to use things not learned yet there.
no loops and for, only if and some other basics.
anyway, i tried in my code to add below the calendar variable at the print :The day of the week is: {calendar}") - i tried to add to the calendar the .day_name and then the variable, but sadly it didnt work...
I guess with my code its impossible to do it, I have to use a new code for it? i mean, restart it.
The for loop is not necessary with your code, I just used it to show an example of getting a different day from a different calendar.weekday
you can modify your existing code to use calendar.day_name
I have pretty much shown you how to do without actually altering your code myself

see this as another example
import calendar

weekday = calendar.weekday(2021, 7, 24)
day_name = calendar.day_name[weekday]
print(day_name)
print(f"Day of the week in english is {day_name}")
Output:
Saturday Day of the week in english is Saturday
Think of calendar.day_name as being a list of each day of the week with Monday at index 0, Tuesday at index 1 etc.
import calendar
    
num = calendar.weekday(2021, 12, 7)
# Constants for weekdays
days = ('MONDAY', 'TUESDAY', 'WEDNESDAY', 'THURSDAY', 'FRIDAY', 'SATURDAY', 'SUNDAY')
print(days[num])
@saidben7243
defining a tuple of weekday strings is not required calendar.day_name already does this.
import calendar

print(*calendar.day_name)
Output:
Monday Tuesday Wednesday Thursday Friday Saturday Sunday
(Jul-24-2021, 07:34 PM)ben1122 Wrote: [ -> ]I guess with my code its impossible to do it, I have to use a new code for it? i mean, restart it.
No you just need to add the user input to what Yoriz has showed you.
I would add a function like to make cleaner,and the point is that no if,elif is needed for this.
import calendar

def day_of_week(year, month, day):
    weekday = calendar.weekday(year, month, day)
    print(f"Day of the week in english is {calendar.day_name[weekday]}")

if __name__ == '__main__':
    year = int(input("Please choose your year: "))
    month = int(input("Please choose your month: "))
    day = int(input("Please choose your day: "))
    day_of_week(year, month, day)
Test.
Output:
Please choose your year: 2021 Please choose your month: 7 Please choose your day: 24 Day of the week in english is Saturday
(Jul-24-2021, 08:56 PM)snippsat Wrote: [ -> ]
(Jul-24-2021, 07:34 PM)ben1122 Wrote: [ -> ]I guess with my code its impossible to do it, I have to use a new code for it? i mean, restart it.
No you just need to add the user input to what Yoriz has showed you.
I would add a function like to make cleaner,and the point is that no if,elif is needed for this.
import calendar

def day_of_week(year, month, day):
    weekday = calendar.weekday(year, month, day)
    print(f"Day of the week in english is {calendar.day_name[weekday]}")

if __name__ == '__main__':
    year = int(input("Please choose your year: "))
    month = int(input("Please choose your month: "))
    day = int(input("Please choose your day: "))
    day_of_week(year, month, day)
Test.
Output:
Please choose your year: 2021 Please choose your month: 7 Please choose your day: 24 Day of the week in english is Saturday

still no definition in course, but it helps to learn, thanks :)
Pages: 1 2