0

No matter what I try I keep getting an infinite loop with this function:

  # Excercise 33 - LPTHW

i = 0 
numbers = []

#Ec 1
#numb = 6
#iplus = 10

def theloop(numb):
        global i
        #i = 0
        #number = []
        while i < numb:
            print "At the top of i is %d" % i
            numbers.append(i)

            i = i + 1
            print "Numbers now: ", numbers
            print "At the bottom i is %d" % i

        print "The numbers: "

        for num in numbers:
            print num

theloop(7)

When I run the script it just keeps printing:

At the top of i is 0
At the top of i is 0
...

Thanks in advance.

3
  • 4
    Is this exactly the script you're running? Because it works fine for me here. Commented Mar 25, 2012 at 1:00
  • Ran the script in Python 3 (correcting the print syntax). Works fine here. Unless numbers.Append() is doing something strange, what you describe cannot happen. Check your indentation. Commented Mar 25, 2012 at 1:06
  • 3
    LPTHW is a crazy way to learn Python. Look at "Dive into Python 3" or one of the decent tutorials. Commented Mar 25, 2012 at 1:15

3 Answers 3

2

Your code works for me as written, but looks to have weird indentation due to use of mixed tabs and spaces. When I read your script using .readlines, you can see this:

 '    def theloop(numb):\n',
 '    \t\tglobal i\n',
 '    \t\t#i = 0\n',
 '            #number = []\n',
 '    \t\twhile i < numb:\n',
 '     \t\t\tprint "At the top of i is %d" % i\n',
 '        \t\tnumbers.append(i)\n',
 '    \n',
 '        \t\ti = i + 1\n',

So I'd recommend switching to four spaces everywhere and having another go. Note the difference in the number of tabs between the print statement and the append/increment statements.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks @DSM that was the issue. Also thanks James, Satoru & Thomas for your insight.
1

if you've mixed spaces and tabs then try to run your script like this:

python -tt yourscript.py ##this will raise error if you've mixed spaces and tabs

this is what I am getting after running your script and it's not infinite.

At the top of i is 0
Numbers now:  [0]
At the bottom i is 1
At the top of i is 1
Numbers now:  [0, 1]
At the bottom i is 2
At the top of i is 2
Numbers now:  [0, 1, 2]
At the bottom i is 3
At the top of i is 3
Numbers now:  [0, 1, 2, 3]
At the bottom i is 4
At the top of i is 4
Numbers now:  [0, 1, 2, 3, 4]
At the bottom i is 5
At the top of i is 5
Numbers now:  [0, 1, 2, 3, 4, 5]
At the bottom i is 6
At the top of i is 6
Numbers now:  [0, 1, 2, 3, 4, 5, 6]
At the bottom i is 7
The numbers: 
0
1
2
3
4
5
6

1 Comment

I will start using -tt great tip.
0

This looks like there is an indenting mistake after the following lines:

   while i < numb:
        print "At the top of i is %d" % i
        numbers.append(i)

Like James said, it works just fine here as you pasted it, so you might want to check if you have the correct indentation level in your actual code.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.