9 Replies - 3091 Views - Last Post: 07 October 2011 - 07:45 AM Rate Topic: -----

#1 TheBudgie  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 15-August 11

How to work with weekdays?

Posted 04 October 2011 - 10:52 AM

Hey there, I'm relatively new to Python, I only really know the basic, and I'm completely new to programming, python being the first language I've tried to learn. At the moment I'm trying to write a program that allows me to define a timetable for each day, and then when the program is run, it checks the day and returns the timetable. It seemed easy enough at first as I have already done a similar thing with hours, but I just can't find out how to do the same thing with days of the week. I've already tried to Google it, but what little does come up doesn't work for me. I'd be grateful if you could help me, and I'm sorry for wasting your time if it's something really obvious.

Is This A Good Question/Topic? 0
  • +

Replies To: How to work with weekdays?

#2 TheBudgie  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 15-August 11

Re: How to work with weekdays?

Posted 04 October 2011 - 11:32 AM

Right ok, I got the weekday thing to work, but my program now gives the weekday followed by "None" or doesn't work, here's the code, please tell me where I'm going wrong:

import calendar
import datetime

now = datetime.datetime.now()

def weekday():
    if calendar.weekday(now.year, now.month, now.day) is 0:
        print "Monday"
    elif 1:
        print "Tuesday"
    elif 2:
        print "Wednesday"
    elif 3:
        print "Thursday"
    elif 4:
        print "Friday"
    elif 5:
        print "Saturday"
    elif 6:
        print "Sunday"
    else:
        print "WTF??"

print "Hello, my name is Rory, and today is", weekday()


Was This Post Helpful? 0
  • +
  • -

#3 Simown  Icon User is offline

  • Blue Sprat
  • member icon

Reputation: 317
  • View blog
  • Posts: 650
  • Joined: 20-May 10

Re: How to work with weekdays?

Posted 04 October 2011 - 02:52 PM

The weekday thing doesn't work, I'm sorry to say, right idea, but the syntax is wrong. I'll explain:

def weekday():
    if calendar.weekday(now.year, now.month, now.day) is 0:
        print "Monday"
    elif 1:
        print "Tuesday"
    elif 2:
        print "Wednesday"



In Python (and most languages) numbers are true, so if it's calendar.weekday(now.year, now.month, now.day) is 0, it's Monday. After that, you are checking elif 1: which is always true, so it will always be Tuesday if it's not Monday. You meant to check: calendar.weekday(now.year, now.month, now.day) is 1.

Your "None" problem, you are printing a function that returns a None value, weekday(). You want to return the values instead of printing them.


import calendar
import datetime

now = datetime.datetime.now()

def weekday():
    if calendar.weekday(now.year, now.month, now.day) is 0:
        return "Monday"
    if calendar.weekday(now.year, now.month, now.day) is 1:
        return "Tuesday"
    ....
print "Hello, my name is Rory, and today is", weekday()


This post has been edited by Simown: 04 October 2011 - 02:54 PM

Was This Post Helpful? 0
  • +
  • -

#4 zem107  Icon User is offline

  • D.I.C Head

Reputation: 7
  • View blog
  • Posts: 84
  • Joined: 10-April 11

Re: How to work with weekdays?

Posted 04 October 2011 - 08:05 PM

You can also make it easier to write by doing this:
import calendar
import datetime

now = datetime.datetime.now()

check = calendar.weekday(now.year, now.month, now.day)

def weekday():
    if check is 0:
        print "Monday"
    elif check is 1:
        print "Tuesday"
    elif check is 2:
        print "Wednesday"
    elif check is 3:
        print "Thursday"
    elif check is 4:
        print "Friday"
    elif check is 5:
        print "Saturday"
    elif check is 6:
        print "Sunday"
    else:
        print "WTF??"
print "Hello, my name is Rory, and today is", weekday()


Just making a simple variable makes it easier to read/write.
After all, Python is about readability and simplicity!
Was This Post Helpful? 0
  • +
  • -

#5 Simown  Icon User is offline

  • Blue Sprat
  • member icon

Reputation: 317
  • View blog
  • Posts: 650
  • Joined: 20-May 10

Re: How to work with weekdays?

Posted 04 October 2011 - 08:14 PM

You can! I was leaving that for the OP to work out :)

This post has been edited by Simown: 04 October 2011 - 08:21 PM

Was This Post Helpful? 0
  • +
  • -

#6 zem107  Icon User is offline

  • D.I.C Head

Reputation: 7
  • View blog
  • Posts: 84
  • Joined: 10-April 11

Re: How to work with weekdays?

Posted 05 October 2011 - 08:00 AM

Whoops, sorry for ruining it.
Was This Post Helpful? 0
  • +
  • -

#7 Simown  Icon User is offline

  • Blue Sprat
  • member icon

Reputation: 317
  • View blog
  • Posts: 650
  • Joined: 20-May 10

Re: How to work with weekdays?

Posted 05 October 2011 - 09:26 AM

Haha, no worries. Your input is just as welcome as anyone elses.
Was This Post Helpful? 0
  • +
  • -

#8 McSick  Icon User is offline

  • D.I.C Head

Reputation: 32
  • View blog
  • Posts: 179
  • Joined: 02-September 10

Re: How to work with weekdays?

Posted 05 October 2011 - 01:06 PM

@Zem107, Be careful because check is not global the way you declared it. This means it will bring up errors being used in the weekday function.

Changing the definition to
def weekday(check):
and calling it in main as
print "Hello, my name is Rory, and today is", weekday(check)
is one way of fixing it ^.^ Oh and of course changing the prints to returns as Simown said earlier.
Was This Post Helpful? 0
  • +
  • -

#9 Simown  Icon User is offline

  • Blue Sprat
  • member icon

Reputation: 317
  • View blog
  • Posts: 650
  • Joined: 20-May 10

Re: How to work with weekdays?

Posted 05 October 2011 - 01:21 PM

Um.. check will be global as it's defined outside any functions and methods. You can test by trying to print it in every scope.

Global variables are usually not the best way to go about it. It could easily be changed to have no globals:

import calendar
import datetime

def weekday():
    now = datetime.datetime.now()
    check = calendar.weekday(now.year, now.month, now.day)
    if check is 0:
        return "Monday"
    elif check is 1:
        return "Tuesday"
    elif check is 2:
        return "Wednesday"
    elif check is 3:
        return "Thursday"
    elif check is 4:
        return "Friday"
    elif check is 5:
        return "Saturday"
    elif check is 6:
        return "Sunday"
    else:
        return "WTF??"

print "Hello, my name is Rory, and today is", weekday()


Was This Post Helpful? 1
  • +
  • -

#10 zem107  Icon User is offline

  • D.I.C Head

Reputation: 7
  • View blog
  • Posts: 84
  • Joined: 10-April 11

Re: How to work with weekdays?

Posted 07 October 2011 - 07:45 AM

check is already global, so it will work. Bud I did forget about the changing the print to return. :whistling:
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1