Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python

Sum of a million function: errors

So I'm working on some of my own ideas, sort of, inspired by treehouse and Crash Course by Matthes.

Anyway trying to get the min() max() and sum() of the range(1,1000001)

Here's what I have:

def sum_million():
    for million in range (1,1000001):
        print (min(million))
        print (max(million))
        print = (sum(million))
print(sum_million())

This keeps giving me a TypeError saying that int is not iterable but when I made my last function to just count from one to a million it worked. The working one is:

def one_million ():

    for million in range (1,1000001):
        print (million)
 print(one_million())

Not sure what I'm doing wrong.

2 Answers

Steven Parker
Steven Parker
243,656 Points

To start with there's a syntax error on this line (a stray equal sign):

        print = (sum(million))

Then, you can't use "min", "max", or "sum" on a single value. Each of those need either multiple arguments, or one that is iterable (like a list). None of them make sense with just one argument.

Perhaps you were thinking of doing this instead (without a loop):

def sum_million():
    million = range(1,1000001)
    print(min(million))
    print(max(million))
    print(sum(million))

sum_million()

So thanks for the response. The chapter I'm basing this on focused on loops and lists. I didn't know you could do it without the loop. Interesting. But do you know a way I could do it with a loop?

Steven Parker
Steven Parker
243,656 Points

Maybe this is what you were thinking?

def sum_million():
    small = 1
    large = 1
    total = 0
    for million in range(1,1000001):
        small = min(small, million)
        large = max(large, million)
        total = sum([total, million])
    print(small, large, total)

sum_million()

Hmmm. It's close I just wanted to use max and min to see if it knew what the maximum and minimum numbers are in the list ( Just 1 and 1000000). And sum is just the sum of one through a million but not listing all the numbers in between in the calculation. Perhaps if I didn't put max and min in the loop.... though I still get an error.

Steven Parker
Steven Parker
243,656 Points

If you only want to see the final results, un-indent the print line. I've updated my sample program to reflect that.

But if you don't want to see the steps, the non-loop version makes more sense.

Thanks! Yeah that's what I was going for.

Sorry one more question, why the number 1 for small and large variables?

Steven Parker
Steven Parker
243,656 Points

Unlike the sum, they need to start with a value that is part of the set. A number outside the set would automatically be either the largest or smallest. They could be any number in the set, 999 or 23456 would work just as well.

Oh okay. Thanks for your help!