Python Forum

Full Version: code works at command line but not in .py
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Good evening....
Can someone please tell me what I am doing wrong....I test this out on the CLI and it seems to work yet when I have it in my .py script I get the following error with respect to line 12:

TypeError: 'int' object is not callable

 #!/usr/bin/env python3
 2 import random
 3 def main():
 4     c = []
 5     d = []
 6     for k in range(14):
 7         tmp = []; sum = 0
 8         for i in range(4):
 9             m = random.randint(0,4)
10             tmp.append(m)
11         c.append(tmp)
12     d = sum(tmp)
13
14     return(c, d)
Thank you!
the script you are putting that code in is probably setting the name "sum" to an int value, as if "sum" were a variable name. you can get away with doing that for any function name you do not call. check your script.
On line 7 you are overwriting the built-in sum function with 0. This is why it is common practice to not use built-in names as variables.
Quote: 7 tmp = []; sum = 0

It is also common practice to not put multiple statements on one line separated with semi-colon as its harder to detect.
it looks like your posted code has line numbers in it starting at line 2. you probably made a mistake pasting the code into your post. put i am pointing that out just in case.
Thank you! I see I have sum as a variable...I totally missed that....