3 Replies - 306 Views - Last Post: 21 March 2013 - 08:01 AM Rate Topic: -----

#1 russel30  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 2
  • Joined: 21-March 13

Problem with Reversing an Array of Numbers

Posted 21 March 2013 - 04:59 AM

I have been trying to reverse an array: A = [3, 2, 1, 5, 9] using a "for loop" and this is the last thing I have tried but it keeps saying that "Traceback (most recent call last):
File "<pyshell#0>", line 1, in <module>
main(ReverseArray)
TypeError: main() takes no arguments (1 given)
:

def ReverseArray():
    [3, 2, 1, 5, 9]
    n = len(A)
    i = 0
    for i in [0, n-1]:
        B = A[i]
        A[i] = A[n - i - 1]

def main():
    A = [13, 21, 15, 38, 49]  # Test case for ReverseArray function
    b = ReverseArray(A)
    print(B)/>

This post has been edited by atraub: 21 March 2013 - 06:10 PM
Reason for edit:: Failed to fix DIC formatting at end of code block :(


Is This A Good Question/Topic? 1
  • +

Replies To: Problem with Reversing an Array of Numbers

#2 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2013
  • View blog
  • Posts: 3,038
  • Joined: 21-June 11

Re: Problem with Reversing an Array of Numbers

Posted 21 March 2013 - 05:11 AM

The error you're getting isn't about the code you defined in your file. It's about the code you type into your Python shell: main(ReverseArray). That code is wrong for the exact reason that the error message is telling you: You've defined main to take no arguments, but you're trying it to call it with one argument. Just call it as main() without anything between the parentheses.
Was This Post Helpful? 1
  • +
  • -

#3 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5643
  • View blog
  • Posts: 12,359
  • Joined: 16-October 07

Re: Problem with Reversing an Array of Numbers

Posted 21 March 2013 - 05:20 AM

# perhaps this should be
def ReverseArray(A):
# def ReverseArray():
    # What the hell is this? Just sitting around?
    # [3, 2, 1, 5, 9]
    n = len(A) # fine
    # i = 0 no, the for loop will load i
    # this will NOT do what you expect
    for i in [0, n-1]:
        # you will loop for two values, 0 and n-1.
        # you want to look into range


Was This Post Helpful? 1
  • +
  • -

#4 russel30  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 2
  • Joined: 21-March 13

Re: Problem with Reversing an Array of Numbers

Posted 21 March 2013 - 08:01 AM

Thankyou
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1