![]() |
write a program which prints the day in english - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: write a program which prints the day in english (/thread-34364.html) Pages:
1
2
|
write a program which prints the day in english - ben1122 - Jul-24-2021 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... RE: write a program which prints the day in english - Yoriz - Jul-24-2021 You can use calendar.day_name to get the day name stringimport calendar print(f"Day of the week in english is {calendar.day_name[0]}")
RE: write a program which prints the day in english - ben1122 - Jul-24-2021 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... RE: write a program which prints the day in english - Yoriz - Jul-24-2021 calendar.weekday returns an int for the weekday, calendar.day_name is indexable using the weekday intimport 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]}")
RE: write a program which prints the day in english - ben1122 - Jul-24-2021 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. RE: write a program which prints the day in english - Yoriz - Jul-24-2021 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}") 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.
RE: write a program which prints the day in english - saidben7243 - Jul-24-2021 import calendar num = calendar.weekday(2021, 12, 7) # Constants for weekdays days = ('MONDAY', 'TUESDAY', 'WEDNESDAY', 'THURSDAY', 'FRIDAY', 'SATURDAY', 'SUNDAY') print(days[num]) RE: write a program which prints the day in english - Yoriz - Jul-24-2021 @saidben7243 defining a tuple of weekday strings is not required calendar.day_name already does this.import calendar print(*calendar.day_name)
RE: write a program which prints the day in english - snippsat - Jul-24-2021 (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.
RE: write a program which prints the day in english - ben1122 - Jul-25-2021 (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. still no definition in course, but it helps to learn, thanks :) |