-1

I'm trying to make a program that would for example if the user inputs the number 10, it would break it into 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 and add all those numbers. My current code is as follows:

x = int(input('Enter a number: '))
for x in range(1,x+1,1):
  print(sum(x))

This is giving me "TypeError: 'int' object is not iterable"

Any help would be appreciated

user3879060
  • 25
  • 1
  • 8

1 Answers1

1

sum takes a list, but you're giving it x, which is an integer. "Find the sum of 1" doesn't make sense to the interpreter.

I don't think you really need the for loop here. Try:

print(sum(range(1, x+1)))
Kevin
  • 74,910
  • 12
  • 133
  • 166
  • What would I do for multiplication though? – user3879060 Aug 15 '14 at 12:48
  • [Python doesn't have a `product` function](http://stackoverflow.com/questions/7948291/is-there-a-built-in-product-in-python), so you would have to write your own. – Kevin Aug 15 '14 at 12:50