Bottom Page

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!
Quote
#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.
What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Quote
#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.
Skaperen likes this post
Quote
#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.
What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Quote
#5
Thank you! I see I have sum as a variable...I totally missed that....
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Script works when executed from command prompt but not when executed in SDP Tippex 0 145 Apr-07-2020, 04:26 PM
Last Post: Tippex
  Taking Multiple Command Line Argument Input bwdu 6 333 Mar-29-2020, 05:52 PM
Last Post: buran
  python 3 from command line Dixon 1 262 Mar-01-2020, 08:35 PM
Last Post: snippsat
  Need explanation of one line of code Fliberty 6 339 Feb-18-2020, 12:50 AM
Last Post: Fliberty
  Translate this line of code please keystone76 2 296 Jan-31-2020, 11:15 PM
Last Post: keystone76
  Running linux command line apps... dbrdh 0 216 Jan-30-2020, 01:14 PM
Last Post: dbrdh
  Generate simple melodic line from code QuarterMissiv 7 442 Jan-09-2020, 05:27 PM
Last Post: QuarterMissiv
  yet another code that works on 2 but not in 3 rick666 1 288 Dec-12-2019, 08:12 PM
Last Post: Gribouillis
  command line input (arg parse) and data exchange Simba 7 734 Dec-06-2019, 11:58 PM
Last Post: Simba
  Code that works on py2 and not in py3 rick666 3 461 Nov-29-2019, 01:37 PM
Last Post: rick666

Forum Jump:


Users browsing this thread: 1 Guest(s)