Saturday, February 9, 2019

codingdirectional: Return the day in a week with python

Hi, I just have a time to solve another python question today. In this example, we are going to develop a method which will receive a number from the user input and returns which day in a week is that numbers refer to. For example, 1 is Sunday and 2 is Monday. If the number is too large or too small then the program will return an error message. Below is the solution to this question, if you have a better solution don’t forget to leave your answer on the below tweet.

def whatday(num):

    index = num -1
    if(index < 0 or index > 6):
        return "Wrong, please enter a number between 1 and 7"
    else:
        return ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"][index]
        

Like the below tweet or follow me on twitter if you will.



from Planet Python
via read more

No comments:

Post a Comment

TestDriven.io: Working with Static and Media Files in Django

This article looks at how to work with static and media files in a Django project, locally and in production. from Planet Python via read...