
Nurbol Y.
asked 11/24/20How to Assume the days of the week are numbered 0,1,2,3,4,5,6 from Sunday to Saturday. Write a function which is given the day number, and it returns the day name (a string).
Plzzz help by tomorrow.
2 Answers By Expert Tutors

Kevin B. answered 11/24/20
30+ years experience programming in diverse languages and OS
It might be useful to know that the question is exploring the concept of associative array or associative memory. Python has a special built-in data structure for this named Dictionary. A Python Dictionary stores a collection of associated data: keys and values. In this example, the keys are the integers, while the values are the names of the days (strings). According to the Python documentation ( https://docs.python.org/3/tutorial/datastructures.html ) the main operations on a Python Dictionary are storing some value with an associated key, and retrieving a value given its associated key. While this example doesn't really require it, in some applications it would be beneficial to use this built-in capability rather than write all the code for inserting, enforcing unique keys, sorting, removing entries etc from scratch. Cheers.
weekDay = {
0: "Sunday",
1: "Monday",
2: "Tuesday",
3: "Wednesday",
4: "Thursday",
5: "Friday",
6: "saturday"
}
def is_valid(input_string):
try:
val = int(input_string)
if val < 0 or val > 6:
return False
return True
except ValueError:
return False
day = input("Enter day number (0-6): ")
isValid = is_valid(day)
while not isValid:
day = input("Invalid input. Enter day number (0-6): ")
isValid = is_valid(day)
n = int(day)
print(weekDay[n])
Still looking for help? Get the right answer, fast.
Get a free answer to a quick problem.
Most questions answered within 4 hours.
OR
Choose an expert and meet online. No packages or subscriptions, pay only for the time you need.
Hai D.
11/24/20