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
  Unable to understand the meaning of the line of code. jahuja73 0 274 Jan-23-2024, 05:09 AM
Last Post: jahuja73
  My code works on Jupyter Lab/Notebook, but NOT on Visual Code Editor jst 4 879 Nov-15-2023, 06:56 PM
Last Post: jst
  Code works but doesn't give the right results colin_dent 2 674 Jun-22-2023, 06:04 PM
Last Post: jefsummers
  Python VS Code: using print command twice but not getting output from terminal kdx264 4 1,035 Jan-16-2023, 07:38 PM
Last Post: Skaperen
  Code used to work 100%, now sometimes works! muzicman0 5 1,384 Jan-13-2023, 05:09 PM
Last Post: muzicman0
  Trying to loop through code to plot seaborn line plots across multiple subplots eyavuz21 0 1,576 Dec-05-2022, 10:46 AM
Last Post: eyavuz21
  Command line argument issue space issue mg24 5 1,280 Oct-26-2022, 11:05 PM
Last Post: Yoriz
  accept command line argument mg24 5 1,242 Sep-27-2022, 05:58 PM
Last Post: snippsat
  Pandas - error when running Pycharm, but works on cmd line zxcv101 1 1,321 Jun-18-2022, 01:09 PM
Last Post: snippsat
  How to use a variable in linux command in python code? ilknurg 2 1,547 Mar-14-2022, 07:21 AM
Last Post: ndc85430

Forum Jump:

User Panel Messages

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