1

I am learning python using STEPIK and I am facing this:

I want on every iteration of the inside for loop sum the values of predicted_growth list with the value of cases list, save it in some temporary variable and print it. Click on the link for the full challenge.

This is my code so far:

def predict_cases(cases, predicted_growth):
    print()
    result = []
    for i in range(len(cases)):
        for j in range(len(predicted_growth)):
            result = cases[i] + predicted_growth[j]
            print(result, end = " ")
        print()


cases = [12508, 9969, 310595, 57409]
predicted_growth = [100, 200, 300]

predict_cases(cases, predicted_growth)

This is the output of the function I should be getting:

[12608, 10069, 310695, 57509]
[12808, 10269, 310895, 57709]
[13108, 10569, 311195, 58009]

Instead I am getting this:

12608 12708 12808 
10069 10169 10269 
310695 310795 310895 
57509 57609 57709 

4 Answers 4

1

You can use a loop with an inner list comprehension.

def predict_cases(cases, predicted_growth):
    res = [] # this will hold the results

    for growth_rate in predicted_growth: # outer loop to iterate the growth rate
        prev = res[-1] if res else cases # get the previously computed result, if there is no such result get initial cases (`cases`) as previously computed result
        res.append([growth_rate + i for i in prev]) # list comprehension to add the current growth rate(`growth_rate`) to the previous cases

    for result in res: # iterate through `res` list
        print(result) # print the result

cases = [12508, 9969, 310595, 57409]
predicted_growths = [100, 200, 300]
predict_cases(cases, predicted_growths)

Output

[12608, 10069, 310695, 57509]
[12808, 10269, 310895, 57709]
[13108, 10569, 311195, 58009]
Sign up to request clarification or add additional context in comments.

2 Comments

That's perfect, but to be honest I don't understand most of the lines. I will have to dive into list comprehension. Thank you
I will add in a comment
0

You just have your for loops inside out:

def predict_cases(cases, predicted_growth):
    print()
    growth = 0
    for j in predicted_growth:
        growth += j;
        result = []
        for i in cases:
            result.append(growth + i)
        print(result)


cases = [12508, 9969, 310595, 57409]
predicted_growth = [100, 200, 300]

predict_cases(cases, predicted_growth)

Output:

[12608, 10069, 310695, 57509]
[12808, 10269, 310895, 57709]
[13108, 10569, 311195, 58009]

Comments

0

Lists are inherently iterable, thus for i in myList: makes it easier. List also have the .append() method, which builds your list easily. Then the aforementioned loop swap to get things in your desired order.

def predict_cases(cases, predicted_growth):

    for j in predicted_growth:
        result = []
        for i in cases:
            result.append(i + j)
        print(result)

cases = [12508, 9969, 310595, 57409]
predicted_growth = [100, 200, 300]
predict_cases(cases, predicted_growth)

Comments

0

First, the order of the nested loops should be reversed in my opinion,as you want to print three lists, corresponding to len(predicted_growth) and not four lists, which would correspond to len(cases).

Also, you are creating the variable result as a list but then you are overwriting it with a number, which is cases[i] + predicted_growth[j], so it will always print the numbers and never the list containing them.

I got the desired output with the following function:

def test(c, p):
    print()
    growth_factor = 0
    result = []
    for i in p:
        growth_factor += i
        for j in c:
            result.append(i + growth_factor)
        print(result)
        result = []

Note that you can iterate in a list as it is shown in the for loops, without the need of specifying list[iterator], which in this example can be more comfortable in my opinion.

2 Comments

Thank you, now it outputs lists, awesome! but the operation isn't simply addition its increment of a predicted_growth list member and cases. Therefore the step is not always 100 but 100, 200, 300 from the previous sum -- > [12608, 10069, 310695, 57509] [12808, 10269, 310895, 57709] [13108, 10569, 311195, 58009]
You are right, my mistake, I read through my output too quickly. I have updated my code with the missing operations in order to get the expected output. You do need a variable which determines the current growth factor in order to properly update the resulting list.

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.