Python Forum
code works at command line but not in .py
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
code works at command line but not in .py
#1
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!
Reply
#2
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.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#3
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.
Recommended Tutorials:
Reply
#4
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.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#5
Thank you! I see I have sum as a variable...I totally missed that....
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Insert command line in script lif 4 985 Mar-24-2025, 10:30 PM
Last Post: lif
  Pandas - error when running Pycharm, but works on cmd line zxcv101 2 2,424 Sep-09-2024, 08:03 AM
Last Post: pinkang
  Simplest way to run external command line app with parameters? Winfried 2 1,242 Aug-19-2024, 03:11 PM
Last Post: snippsat
  Printing the code line number arbiel 6 1,632 Jun-30-2024, 08:01 AM
Last Post: arbiel
  Unable to understand the meaning of the line of code. jahuja73 0 1,027 Jan-23-2024, 05:09 AM
Last Post: jahuja73
  My code works on Jupyter Lab/Notebook, but NOT on Visual Code Editor jst 4 4,287 Nov-15-2023, 06:56 PM
Last Post: jst
  Code works but doesn't give the right results colin_dent 2 1,405 Jun-22-2023, 06:04 PM
Last Post: jefsummers
  Python VS Code: using print command twice but not getting output from terminal kdx264 4 2,123 Jan-16-2023, 07:38 PM
Last Post: Skaperen
  Code used to work 100%, now sometimes works! muzicman0 5 2,743 Jan-13-2023, 05:09 PM
Last Post: muzicman0
  Trying to loop through code to plot seaborn line plots across multiple subplots eyavuz21 0 2,791 Dec-05-2022, 10:46 AM
Last Post: eyavuz21

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020