-2

I tried writing the code for a problem, but the module won't run. It says invalid syntax, but it's not highlighting anything.

The code: http://pastebin.com/cJVNBcYE

The problem: http://pastebin.com/p8E0E0Nj

I don't understand why it's not working.

I have numDealers set as a variable so that info can be entered in the program. The arrays are all defined. I have index=0 and x=1 to set up the loop for the numDealer arrays for sales and commission. I have another array=index section to calculate commissions. And then I have the prints set up.

Why isn't the program working? I don't understand.

1 Answers1

0

Please post code in future, with a full traceback of the error. However:

else print(sales[index]) and print(comm[index])

should be:

else:
    print(sales[index]) and print(comm[index])

i.e. you are missing a colon

I'm a bit puzzled by the and. It means that the second print will only be executed if the first fails (unlikely). Did you mean:

else:
    print(sales[index])
    print(comm[index])

?

By the way, it appears you are not using arrays but lists. The Python standard library includes a module called array https://docs.python.org/3/library/array.html which you do not appear to be using. So don't have a list called array, that collides with the standard library module name, and can cause no end of confusion.

cdarke
  • 42,728
  • 8
  • 80
  • 84
  • Thanks. It at least runs now, but with an error message. "line 28, in print(sales[index]) IndexError: list index out of range" What does out of range mean? – user179627 Mar 10 '16 at 15:32
  • Also, I'm not sure what you mean about arrays and lists. I had another assignment, which the professor helped me solve. Linking to it, since it's too long for the comment box here: http://pastebin.com/sFRYv331 That program runs fine. Does it use arrays or lists? – user179627 Mar 10 '16 at 15:37
  • It uses lists. Your professor is using the term 'array' generically. In most languages these structures are called arrays, just not in python. In python the built-in mutable sequential type is called a list, and the immutable sequential type is called a tuple. See http://stackoverflow.com/questions/176011/python-list-vs-array-when-to-use. – cdarke Mar 10 '16 at 15:57
  • The index is a numeric position. Your 'index out of range' means that the value of `index` is either too large or too small (negative). Without debugging your code, the most common issue is to forget that (in python) the first (lowest) index number is zero. So, if you have 5 elements in a list the highest valid index is 4. – cdarke Mar 10 '16 at 16:01
  • You have a bunch of statements near the end of your code that are outside the `for` loop because of no indentation. It appears they should be *inside* the `for` loop by indenting. But you have bigger problems... – cdarke Mar 10 '16 at 16:10
  • ... you are setting the variable `array` (bad name) to zero, so how large will the list be? You are setting it to zero elements- empty! Consider what `array` should be, `numDealers`? – cdarke Mar 10 '16 at 16:12