0

Is there a way I can store all of the return values from a function from each time I call it? Here is the code:

def workdaystart(dayoftheweek):
    starthour=input("What is your START HOUR for "+ dayoftheweek+"? ")
    print("Is that AM or PM")#Allows us to differentiate between time periods#
    print ("1. AM")
    print ("2. PM")
    print("3.I DONT WORK")
    starthoursuffix = int(input('Enter your choice [1-2] : '))

    if starthoursuffix == 1:

        starthour=starthour+"AM"
    elif starthoursuffix==2:
        starthour=starthour+"PM"
    else:
        starthour=" "
    return starthour
daysofweek=
    ["monday","tuesday","wednesday","thursday","friday","saturday","sunday"]
for day in daysofweek:
    x=workdaystart(day)

As you can see it runs the items in the list in the function but I then want the start hour of that day to be stored as a variable outside of the funtion.

2
  • 1
    That's what x=workdaystart(day) does, it stores the start hour in the variable x. Commented Oct 26, 2017 at 1:11
  • But it will keep being overridden. After the loop, the value of x would be the value of Sunday's. Create a dictionary with the days as the keys and the start hours as values Commented Oct 26, 2017 at 1:52

4 Answers 4

1

It sounds like you're looking for a dictionary that maps day names to start hours:

starthours = {}
for day in daysofweek:
    starthours[day] = workdaystart(day)
Sign up to request clarification or add additional context in comments.

Comments

0

I read this question as such, you want to store all the start hours in a variable, while with your current code, x is overwritten everytime you loop through the function.

How about using a dictionary? You can then retrieve it according to the day at will.

def workdaystart(dayoftheweek):
    starthour=input("What is your START HOUR for "+ dayoftheweek+"? ")
    print("Is that AM or PM")#Allows us to differentiate between time periods#
    print ("1. AM")
    print ("2. PM")
    print("3.I DONT WORK")
    starthoursuffix = int(input('Enter your choice [1-2] : '))

    if starthoursuffix == 1:
        starthour=starthour+"AM"
    elif starthoursuffix==2:
        starthour=starthour+"PM"
    else:
        starthour=" "
    return starthour

daysofweek["monday",...]
workdaydictionary = {}

for day in daysofweek:
    workdaydictionary[day] = workdaystart(day)

Comments

0

You can generate a list of the returned starthour values:

daysofweek = ["monday","tuesday","wednesday","thursday","friday","saturday","sunday"]
hours = [workdaystart(day) for day in daysofweek]

Comments

0

The Barmar's answer is very useful. You can understand a list or dictionary as mutable data structures where new values can be easily added and can be accessed by index or by key. If you want to do it inside a function, it is better to rename it clearly and to add the dictionary starthours as a parameter.

def edit_workdaystart(dayoftheweek, starthours):
    ... the same
    ...
    starthours[dayoftheweek] = starthour
    # return starthour  # can be omitted

starthours = {}
for day in daysofweek:
    edit_workdaystart(day, starthours)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.