Your sum is correct, just do not use sum as function name. You just need to make user input function, look code snippets section for examples.
pyTony
pyMod
6,310 posts since Apr 2010
Reputation Points: 879
Solved Threads: 987
Skill Endorsements: 26
Avoid using names like list and sum since they are internal to Python. Something like this will work:
mylist = []
prompt = "%d) enter an integer (-99 to stop): "
for k in range(1000):
x = int(raw_input(prompt % (k+1)))
if x == -99:
break
mylist.append(x)
print(mylist) # test
'''
1) enter an integer (-99 to stop): 4
2) enter an integer (-99 to stop): 66
3) enter an integer (-99 to stop): 7
4) enter an integer (-99 to stop): 12
5) enter an integer (-99 to stop): -99
[4, 66, 7, 12]
'''
bumsfeld
Nearly a Posting Virtuoso
1,495 posts since Jul 2005
Reputation Points: 409
Solved Threads: 235
Skill Endorsements: 1
Sorry, the code areas are still rather goofy in the 'new improved' DaniWeb
bumsfeld
Nearly a Posting Virtuoso
1,495 posts since Jul 2005
Reputation Points: 409
Solved Threads: 235
Skill Endorsements: 1
For fun here is recursive function producing sum of numbers without remembering the numbers:
def my_sum():
n = raw_input('Next number or empty to finish: ')
return (float(n) + my_sum()) if n else 0
s = my_sum()
print "The sum of numbers is", s
Report any not working code sections by reporting them by Flag Bad Post link.
Do not select code snippet from type of article but type for example to search box input snippet and to Forum Python. The limiting from menu to Code Snippet does not work same time with Forum selected.
pyTony
pyMod
6,310 posts since Apr 2010
Reputation Points: 879
Solved Threads: 987
Skill Endorsements: 26