# Nested Loop Problem

Page 1 of 1

## 2 Replies - 1133 Views - Last Post: 15 January 2013 - 04:44 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=307335&amp;s=cd5dacb14cda4be4641b0886dd821d67&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 chan 06

Reputation: 2
• Posts: 67
• Joined: 07-October 12

# Nested Loop Problem

Posted 15 January 2013 - 03:26 AM

Well I am having a problem with the condition loops, since I nested loop apparently I would need two nested loops in order to make sure that I would be able to find the ratios inside of an array.

```userinput, num1, store_ratios, count = [], 0, [], 0

count = int(input("How many numbers would you like to enter?"))

for i in range (0,count+1):
num1 = int(input("Enter a number:"))
userinput.append(num1)
for j in range (count+1, 0, -1):
ratio = userinput[i]/userinput[j]
store_ratios.append(ratio)
print (store_ratios)

```

The problem I am having is with the nested loop and trying to find the ratio from the last one to the first one. For example:

ratio list = []
array_list = [3, 6, 12, 24, 48, 96]

Take the last array and divide the second last array and store in another array:

ratio = array_list[5]/array_list[4]

ratio_list.append(ratio)

So any ideas, that you could help me to fix the nested loop?

Is This A Good Question/Topic? 0

## Replies To: Nested Loop Problem

### #2 darek9576

• D.I.C Lover

Reputation: 203
• Posts: 1,731
• Joined: 13-March 10

## Re: Nested Loop Problem

Posted 15 January 2013 - 04:15 AM

Why dont you use functions. If you use function for a well defined task, it's easier to test your code. For example, in your code, you ask the user how many numbers they want to enter. If i put that i want to enter 5 numbers, actually i will have to enter 6. That means that something is wrong and you need to debug it.

If you were to write that as a function, you could pass as a parameter the size of the list and inside the function create the list and return it. You test it, you move on.

The second function could calculate the ratios based on the list that is passed in to it. It takes a list, size does not matter since you can figure it out and perform necessary calculations.

I am not sure whether the ratios function does what you want but i tried it. Performance is not great since i cheated by reversing the list. For better performance, dont reverse it, just count indexes backwards.

```def initializeList(size):
lst = []
for i in range(size):
value = int(input("Enter a number:  "))
lst.append(value)
return lst

def createRatiosList(lst):
ratiosList = []
listCopy = lst[:]
listCopy.reverse()
length = len(listCopy)

i = 0
j = 1
while j < length:
number = listCopy[i] + listCopy[j]
ratiosList.append(number)
i = i + 1
j = j + 1
return ratiosList

value = int(input("How many values do you want to input?:   "))
lst = initializeList(value)
lst2 = createRatiosList(lst)

print(lst)
print(lst2)

```

### #3 andrewsw

• RequestedRangeNotSatisfiable

Reputation: 6554
• Posts: 26,569
• Joined: 12-December 12

## Re: Nested Loop Problem

Posted 15 January 2013 - 04:44 AM

If your code is indented as appears in your first post then you are not performing a nested loop. The second for loop needs to be tabbed in:

```userinput, num1, store_ratios, count = [], 0, [], 0

count = int(input("How many numbers would you like to enter?"))

for i in range (0,count+1):
num1 = int(input("Enter a number:"))
userinput.append(num1)
for j in range (count+1, 0, -1):
ratio = userinput[i]/userinput[j]
store_ratios.append(ratio)
print (store_ratios)

```

This seemed an obvious point to mention so I haven't looked at your code beyond this.

This post has been edited by andrewsw: 15 January 2013 - 04:48 AM

Page 1 of 1

 .related ul { list-style-type: circle; font-size: 12px; font-weight: bold; } .related li { margin-bottom: 5px; background-position: left 7px !important; margin-left: -35px; } .related h2 { font-size: 18px; font-weight: bold; } .related a { color: blue; }