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
Mikaela Land
Python Web Development Techdegree Student 2,324 PointsSum 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
243,656 PointsTo 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()
Mikaela Land
Python Web Development Techdegree Student 2,324 PointsHmmm. 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
243,656 PointsIf 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.
Mikaela Land
Python Web Development Techdegree Student 2,324 PointsThanks! Yeah that's what I was going for.
Sorry one more question, why the number 1 for small and large variables?
Steven Parker
243,656 PointsUnlike 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.
Mikaela Land
Python Web Development Techdegree Student 2,324 PointsOh okay. Thanks for your help!
Mikaela Land
Python Web Development Techdegree Student 2,324 PointsMikaela Land
Python Web Development Techdegree Student 2,324 PointsSo 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
243,656 PointsSteven Parker
243,656 PointsMaybe this is what you were thinking?