Nested loop with combination of "for" and "while"

980 Views Asked by At

My Python Script:

app = "google facebook yahoo"
prec = 0
test = 0
while test < 4 :
    print "The test is test%d" % (test)
while prec < 4 :
    prec = prec + 1
for i in app.split():
    print "The word is " + (i) + " precedence %d" % (prec)

Actual Output now:

Its printing continuously the following

The test is test0
The test is test0
The test is test0
The test is test0
The test is test0
The test is test0

Expected Output:

The test1 is
The word is google precedence 1    
The word is facebook precedence 2
The word is yahoo precedence 3

The test2 is
The word is google precedence 1    
The word is facebook precedence 2
The word is yahoo precedence 3

The test3 is
The word is google precedence 1    
The word is facebook precedence 2
The word is yahoo precedence 3

The test4 is
The word is google precedence 1    
The word is facebook precedence 2
The word is yahoo precedence 3

Please guide me on how to achieve this output. Thanks in advance.

3

There are 3 best solutions below

1
On BEST ANSWER

One problem is that your loops are not nested. The next thing is that your first loop

while test < 4 :
    print "The test is test%d" % (test)

is an infinite loop, because your variable "test" is set to 0 and that never changes in the loop. So test<4 is always true.

You could do something like that.

apps = "google facebook yahoo"
for i in range (1,4):
    print 'test' + str(i) + 'is...'
    precedence = 1
    for app in apps.split():
        print "The word is " + app + " precedence " + str(precedence)
        precedence += 1 
2
On

There are no changes to those variables within your while loops, so their value never changes and the loop never exits.

This should work:

apps = ['google', 'facebook', 'yahoo']

for i in xrange(4):
    print 'test' + str(i) + 'is...'
    for app in apps:
        print "The word is " + app + " precedence %d" % i
0
On

Ok I tried to fix it by modifying your original code as little as possible.

First off any time you use a while loop you need to make sure it can be broken out of. While loop basically means "run this task until you reach a certain condition". In your case your while loop is going to run until test is greater than or equal to 4. So in order to break I added the following code at the beginning of the loop.

test += 1

the += is just shorthand for test = test + 1

Once test reaches 4 the program will exit the while loop.

The second while loop was not needed in the code because you already had a for loop that iterated over the string. In this case it was much simpler to just remove the second while and place the prec counter inside of the for loop. In order to make sure that the counter is reset for each loop I moved prec = 0 inside of the while loop but outside of the for loop. This was so each time the for loop runs prec starts at 0 and is then incremented to 1,2,3 and then back to 0.

Hope that helps!

#!/usr/bin/python
app = "google facebook yahoo"
test = 0
while test < 4 :
    test += 1
    print "The test is test%d" % (test)
    prec = 0
    for i in app.split():
        prec = prec + 1
        print "The word is " + (i) + " precedence %d" % (prec)