0

I've managed to get this function to iterate over the list. What I want is a way to include the current list element in the output string. ie. (raw_input("Enter the number of hours the employee worked on : ")) showing Monday or Tuesday or which ever element the function is at.

# -*- coding: utf-8 -*-

day = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"]

def hours():
    while True:
        try:
            hours = int(raw_input("Enter the number of hours the employee worked on : "))
            if (hours >=0) and (hours <=24):
                return hours
            else:
                print ("Please enter a whole number that is more than zero & less than 24")
        except ValueError:
            print ("Please enter a whole number that is more than zero & less than 24")
            continue


for i in day:
    print hours()
styvane
  • 59,869
  • 19
  • 150
  • 156
Chopwise
  • 13
  • 2
  • in case I have any more questions. can someone also explain why my code is all bunched up like that – Chopwise Mar 29 '16 at 06:17
  • you should consider to read [How do I format my code blocks?](https://meta.stackexchange.com/questions/22186/how-do-i-format-my-code-blocks). That being said what do you mean by *include the current list element in the output string*? Please add the expected output to your question – styvane Mar 29 '16 at 06:22
  • 1
    you have to pass a parameter to hours function : def hour(variable): – SnakeFoot Mar 29 '16 at 06:22
  • what version your using? – Francisunoxx Mar 29 '16 at 06:22
  • 1
    judging from the parentheses in print, i would say is 3.x – SnakeFoot Mar 29 '16 at 06:25
  • i'm using 2.7. what I want is for user input for each day of the week. i.e. enter hours for mon, then tues, wed & so on. i'm not sure how to reference the list element in the input string – Chopwise Mar 29 '16 at 06:32

2 Answers2

1

If you want the weekday in your output you can use the format() function like so:

"Enter ... worked on {0} : ".format(day)

and pass the day into your hours() function:

print hours(i)

Of course, you have to define your function to accept that one new parameter:

def hours(day):
    ...

So your function with a little bit of clean-up looks as follows. Notice that the list of weekdays has a capitalized name as per convention for constants, and that the range check is simplified. Also, in general, if you have to type the same line of code more than once it might make sense to rethink the structure of your code:

def hours(day):
    while True:
        try:
            prompt = "Enter the number of hours the employee worked on {0}: ".format(day)
            hours = int(raw_input(prompt))
            if 0 <= hours <= 24:
                return hours
        except ValueError:
            pass # Silently catch failure.
        print "Please enter a whole number that is more than zero & less than 24"

WEEKDAYS = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"]
for day in WEEKDAYS:
    print hours(day)
Community
  • 1
  • 1
Jens
  • 8,423
  • 9
  • 58
  • 78
0

If you're using python 3. Just make it hours = int(input("Enter the number of hours the employee worked on: "))

Francisunoxx
  • 1,440
  • 3
  • 22
  • 45