I'm having a good deal of trouble figuring out how to sum the values of a list until they reach a specified goal. This is a H.W. assignment and it is as follows:

"Define a Python function threshold(dailyGains, goal) that behaves as follows. The first parameter is a list of integers that represent the daily gains in the value of a stock. The second paramter is a single positive number that is a profit goal. The function should return the minimum number of days for which the stock should be owned in order to achieve a profit that is equal to or greater than the stated goal. If the goal is unreachable, the function should return 0."

My own attempt is very muddled and I'm not usually so flummoxed. Sorry for any 'newbie' errors, but I could really appreciate some help:

###
goal = int(raw_input("Enter profit goal: "))
dailyGains = [3, 2, 4, 8, 5, 6, 9]
days = 0
profit = 0
newlist = []
def threshold(dailyGains, goal):
[x + x for x in dailyGains]
for x in dailyGains:
newlist.append(x + x)
print newlist

print threshold(dailyGains, goal)###

Somewhere in the function you will have to add x (list item value) to a cumulative total and compare it to goal. If total is greater than goal then return number of days (can also be a total or the for loop index+1 or iter if you use a for loop, whichever of the options is most comfortable).

Well, my ineptitude has become quite durable as I'm still totally lost and a deadline looms. The 'append' function is beyond the scope of the assignment's intentions, so it should be accomplished with what I've assembled below, but I just can't figure out how to arrange it correctly. Pardon my newbie idiocy. Any suggestions would be appreciated.

goal = int(raw_input("Enter profit goal: "))
dailyGains = [3, 2, 4, 8, 5, 6, 9]

def threshold(dailyGains, goal):
    days = 0
    profit = 0
    while goal >= profit:
        for value in dailyGains:
            profit = value + 1
            days = days + 1
            return days
            print days
            
    
print threshold(dailyGains, goal)

You want to seriously consider studying the "Starting Python" thread at the top of the forum, especially the third post (I think) which explains returning a value from a function. You obviously are not getting enough out of class and will have to compliment class with outside work. The following returns the number of days needed to exceed the goal but is not a complete solution

def threshold(dailyGains, goal):
    days = 0
    profit = 0
    for each_value in dailyGains:
            profit += each_value
            days += 1
            if profit > goal:
               return days
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.