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?
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))
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.
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.
no need of line 2: day_of_the_week = n
Hello again Buran
Thanks for this. I've learnt a lot from this experience.:-)
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)}')
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]
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