Python Forum
write a program which prints the day in english
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
write a program which prints the day in english
#1
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]
Yoriz write Jul-24-2021, 06:34 PM:
Please post all code, output and errors (in their entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Reply
#2
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
Reply
#3
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...
Reply
#4
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
Reply
#5
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.
Reply
#6
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.
Reply
#7
import calendar
    
num = calendar.weekday(2021, 12, 7)
# Constants for weekdays
days = ('MONDAY', 'TUESDAY', 'WEDNESDAY', 'THURSDAY', 'FRIDAY', 'SATURDAY', 'SUNDAY')
print(days[num])
Reply
#8
@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
Reply
#9
(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
Reply
#10
(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 :)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  zfill prints extra et the end of a var tester_V 4 904 Mar-24-2023, 06:59 PM
Last Post: tester_V
  (python) Can i get some help fixing a English to Morse translator? Pls AlexPython 7 1,605 Sep-12-2022, 02:55 AM
Last Post: AlexPython
  variable prints without being declared. ClockPillow 2 1,816 Jul-11-2021, 12:13 AM
Last Post: ClockPillow
  More non english characters johnboy1974 8 4,473 Apr-23-2021, 02:35 PM
Last Post: snippsat
  Output prints Account.id at the end? LastStopDEVS 5 2,788 Dec-19-2020, 05:59 AM
Last Post: buran
  I have an index error inline 76 but I write the program in a way that cant reach tha abbaszandi 2 2,064 Nov-13-2020, 07:43 AM
Last Post: buran
  Try/Exept prints only ones tester_V 11 3,892 Nov-03-2020, 02:38 AM
Last Post: tester_V
  loop only prints last character. mcmxl22 1 1,721 Feb-17-2020, 02:36 AM
Last Post: menator01
  How to write a script to execute a program need passing additional input? larkypython 2 2,531 Nov-23-2019, 04:38 AM
Last Post: larkypython
  English interpretation of the following file handing snippet mortch 5 3,188 May-30-2019, 08:10 AM
Last Post: mortch

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020