Python Forum
Programme will not returns the day number not the day name
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Programme will not returns the day number not the day name
#1
I have the following programme:
n = input("Enter a number 0 to 6: ")
day_of_the_week = n

def day_of_the_week ():
    if n == 0:
        return "Sunday"
    elif n == 1:
        return "Monday"
    elif n == 2:
        return "Tuesday"
    elif n == 3:
        return "Wednesday"
    elif n ==4:
        return "Thursday"
    elif n == 5:
        return "Friday"
    elif n == 6:
        return "Saturday"
    else:
        return "Invalid number"

print("The day of the week is ", n)
The out put I get is:
Output:
Python 3.8.4 (tags/v3.8.4:dfa645a, Jul 13 2020, 16:46:45) [MSC v.1924 64 bit (AMD64)] on win32 Type "help", "copyright", "credits" or "license()" for more information. >>> = RESTART: C:/Users/John/Documents/John''s files/Work/Coding/Think Like a Computer Scientist/Programmes/day_of_the_week.py Enter a number 0 to 6: 4 The day of the week is 4 >>>
Could anyone please advise what I am doing wrong?
Reply
#2
You are only defining the function day_of_the_week(), which by the way should take a parameter day_of_the_week(n), but you are not using it. Functions need to be called to work. Here is how to do it:
print("The day of the week is ", day_of_the_week(n))
Reply
#3
Also, note that input will return str while you compare n (which is global variable used in the function you define - bad practice) with int, so even if you implement @Gribouillis suggestion the result will not be as expected.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#4
Hello Gribouillis

Thank you for your prompt reply and I have made the changes you suggest but still do not get the output as the day name. Please see below

n = input("Enter a number 0 to 6: ")
day_of_the_week = n

def day_of_the_week (n):
    if n == 0:
        return "Sunday"
    elif n == 1:
        return "Monday"
    elif n == 2:
        return "Tuesday"
    elif n == 3:
        return "Wednesday"
    elif n == 4:
        return "Thursday"
    elif n == 5:
        return "Friday"
    elif n == 6:
        return "Saturday"
    else:
        return "Invalid number"

print("The day of the week is ", day_of_the_week(n))
= RESTART: C:/Users/John/Documents/John''s files/Work/Coding/Think Like a Computer Scientist/Programmes/day_of_the_week.py
Enter a number 0 to 6: 2
The day of the week is Invalid number
>>>
And the output is:

Output:

Hello Gribouillis and Buran

Thanks for all your help problem solved and I understand why.
Reply
#5
no need of line 2: day_of_the_week = n
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#6
Hello again Buran

Thanks for this. I've learnt a lot from this experience.:-)
Reply
#7
You can take advantage of using dict in order to avoid long/multiple if/elif/else
def day_of_the_week(n):
    week = {'0':'Sunday',
            '1':'Monday',
            '2':'Tuesday', 
            '3':'Wednesday',
            '4':'Thursday',
            '5':'Friday',
            '6':'Saturday'}
    return week.get(n, 'Invalid number')

n = input("Enter a number 0 to 6: ")
print(f'The day of the week is {day_of_the_week(n)}')
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#8
You can use calendar.day_name or calendar.day_abbr to get the day name in your language.
The benefit is, that already other languages are supported and included.

The method date.weekday() return the weekday.
The datetime object has this method also.
import calendar
import datetime
import locale


def setlocale(language):
    locale.setlocale(locale.LC_ALL, language)


def get_day_name(date=None):
    if date is None:
        date = datetime.datetime.now()
    weekday = date.weekday()
    return calendar.day_name[weekday]


tomorrow = datetime.timedelta(days=1)
# used to do calculation with dates
# can be added or subtracted from date or datetime object
# supports also multiplication and division
# a half day == tomorrow / 2


print("English")
print("Today:", get_day_name())
print("Tomorrow:", get_day_name(datetime.date.today() + tomorrow))

setlocale("de")
print("Deutsch")
print("Heute:", get_day_name())
print("Morgen:", get_day_name(datetime.date.today() + tomorrow))


setlocale("zh")
print("中文")
print("今天:", get_day_name())
print("明天:", get_day_name(datetime.date.today() + tomorrow))
Output:
English Today: Monday Tomorrow: Tuesday Deutsch Heute: Montag Morgen: Dienstag 中文 今天: 星期一 明天: 星期二
A minimal implementation with your function:
import calendar


def day_of_the_week(n):
    if not 0 <= n <= 6:
        raise ValueError("Illegal weekday number")
    return calendar.day_name[n]
And if you still want to return a text, even if the value is not valid:

import calendar


def day_of_the_week(n):
    if not 0 <= n <= 6:
        return "Illegal weekday number"
    return calendar.day_name[n]
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#9
Hello Dead_Eye

Thank you for this I am learning all the time. Currently I am using 'How to Think Like a Computer Scientist' to teach myself Python and your solution is way ahead of where my learning has got to. Nevertheless, I have made a record and learned from it as I am sure it will help me in my later learning.

Thanks again
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Call a bash script from within a Python programme Pedroski55 6 2,450 Dec-06-2021, 01:53 PM
Last Post: DeaD_EyE
  Can you tell me if this python programme will get me in trouble with my organisation? abstraction 8 3,723 Feb-25-2019, 12:13 PM
Last Post: metulburr
  [Regex] Findall returns wrong number of hits Winfried 8 5,806 Aug-23-2018, 02:21 PM
Last Post: Winfried

Forum Jump:

User Panel Messages

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