4 Replies - 311 Views - Last Post: 12 February 2014 - 11:09 AM Rate Topic: -----

#1 Mike_23  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 03-July 13

Python Weekday program def function issue

Posted 12 February 2014 - 10:33 AM

Ok the assignment is to have a function with a list of the days of the week and have that function print out each day of the week that is in the list. After that, we are to take a user input between 1 and 7 and print the corresponding day of the week using the list function. My issue is using the def function to output all the days of the week automatically. Instead of giving me the days of the week, i get a weird output that seems to be made up of random letters and numbers then the program works correctly. I have tried using for and while loops, but still get the same random number. Any and all help solving this one line of code is greatly appreciated

Week = ['Sunday' , 'Monday' , 'Tuesday', 'Wensday', 'Thursday', 'Friday', 'Saturday']

#the problem starts here
def Days():
    for i in Week:
        print(i)
#the issue ends here

print (Days)
choice = int(input("Enter a number of the day of the week "))


if choice > 7:
   print("Please enter a valid number between 1 and 7")

elif choice == 1:
    print("The 1st day of the week is " , Week[0])
elif choice == 2:
    print("The 2nd day of the week is " , Week[1])
elif choice == 3:
    print("The 3rd day of the week is " , Week[2])
elif choice == 4:
    print("The 4th day of the week is " , Week[3])
elif choice == 5:
    print("The 5th day of the week is " , Week[4])
elif choice == 6:
    print("The 6th day of the week is " , Week[5])
elif choice == 7:
    print("The 7th day of the week is " , Week[6])



Is This A Good Question/Topic? 0
  • +

Replies To: Python Weekday program def function issue

#2 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3371
  • View blog
  • Posts: 11,420
  • Joined: 12-December 12

Re: Python Weekday program def function issue

Posted 12 February 2014 - 10:37 AM

print (Days)

This isn't calling your function, just printing the default representation of the function. Calling/ using the function requires brackets ():
print (Days())

This post has been edited by andrewsw: 12 February 2014 - 10:38 AM

Was This Post Helpful? 1
  • +
  • -

#3 Mike_23  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 03-July 13

Re: Python Weekday program def function issue

Posted 12 February 2014 - 10:51 AM

View Postandrewsw, on 12 February 2014 - 10:37 AM, said:

print (Days)

This isn't calling your function, just printing the default representation of the function. Calling/ using the function requires brackets ():
print (Days())


By god i am blind i didnt see that. Thanks alot
Was This Post Helpful? 0
  • +
  • -

#4 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3371
  • View blog
  • Posts: 11,420
  • Joined: 12-December 12

Re: Python Weekday program def function issue

Posted 12 February 2014 - 10:55 AM

No worries
Was This Post Helpful? 0
  • +
  • -

#5 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 7649
  • View blog
  • Posts: 12,902
  • Joined: 19-March 11

Re: Python Weekday program def function issue

Posted 12 February 2014 - 11:09 AM

Andrew is correct. Remember that a function is an object in python.




Now about the rest of your code. I know you're new to python, but that long blob of ifs is making me crazy. Let me show you another way to do that:

suffixen = {0:"st", 1:"nd", 3:"rd"}
print ("The %d%s day of the week is %s" %(choice+1, suffixen.get(choice, "th"), Week[choice]))


The first line here defines the third special-case suffixes, for the first second, and third ordinals. The second line provides a template, and populates the parts of it which vary according to the data provided. (this is sometimes called a format string) %d and %s mean "fill in an int" and "fill in a string", respectively. They get those ints and strings from the stuff at the end, within the %( ). So in this particular case, we're filling in the number choice, bumping it up by one because you have that offset, and the suffix associated with that number (I'm using the get method here, which allows me to specify a default value - if the value of choice isn't 0 or 1 or 2, it's going to return "th", which is what we want), and the English name of the particular day we're interested in (this is just a list lookup, the way you already did)

I realize that's going to look a little complicated, so you might want to take a little time to try to understand all of the pieces. The thing to notice - the motivator here - is that not only did I get this down to one line, that line expresses exactly what we want to do. It also does fine if we travel to another planet with an eight-day week, or go back to the French revolution, with its ten-day week. All we have to do then is replace the list of days and maybe add any needed suffixen.

So there's some good reasons to try to understand the different pieces of this. It's dense, but worth it.

EDIT: Fixed the suffix list.

This post has been edited by jon.kiparsky: 12 February 2014 - 11:12 AM

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1