2 Replies - 505 Views - Last Post: 15 January 2013 - 04:44 AM Rate Topic: -----

#1 chan 06  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 63
  • 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  Icon User is offline

  • D.I.C Lover

Reputation: 198
  • View blog
  • Posts: 1,691
  • 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)





Was This Post Helpful? 2
  • +
  • -

#3 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3526
  • View blog
  • Posts: 12,041
  • 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

Was This Post Helpful? 2
  • +
  • -

Page 1 of 1