# Problem with Reversing an Array of Numbers

Page 1 of 1

## 3 Replies - 690 Views - Last Post: 21 March 2013 - 08:01 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=316198&amp;s=8d28bf6f5e18c40b74fa9a98a278a7af&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 russel30

Reputation: 1
• 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

• D.I.C Lover

Reputation: 2579
• Posts: 4,123
• 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.

### #3 baavgai

• Dreaming Coder

Reputation: 7153
• Posts: 14,898
• 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

```

### #4 russel30

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

## Re: Problem with Reversing an Array of Numbers

Posted 21 March 2013 - 08:01 AM

Thankyou