3

I'm trying to teach myself python right now, and I'm using exercises from "Learn Python The Hard Way" to do so.

Right now, I'm working on an exercise involving while loops, where I take a working while loop from a script, convert it to a function, and then call the function in another script. The only purpose of the final program is to add items to a list and then print the list as it goes.

My issue is that once I call the function, the embedded loop decides to continue infinitely.

I've analyzed my code (see below) a number of times, and can't find anything overtly wrong.

def append_numbers(counter): i = 0 numbers = [] while i < counter: print "At the top i is %d" % i numbers.append(i) i += 1 print "Numbers now: ", numbers print "At the bottom i is %d" % i count = raw_input("Enter number of cycles: ") print count raw_input() append_numbers(count) 

4 Answers 4

15

I believe you want this.

count = int(raw_input("Enter number of cycles: ")) 

Without converting the input to integer, you end up with a string in the count variable, i.e. if you enter 1 when the program asks for input, what goes into count is '1'.

A comparison between string and integer turns out to be False. So the condition in while i < counter: is always False because i is an integer while counter is a string in your program.

In your program, you could have debugged this yourself if you had used print repr(count) instead to check what the value in count variable is. For your program, it would show '1' when you enter 1. With the fix I have suggested, it would show just 1.

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

Comments

1

convert the input string to integer... count=int(count)

Comments

-1

Above has been cleared why the while looped for ever. It loops for a lot(=ASCII code which is really big)

But to fix the while you can simply:

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

Comments

-4

raw_input returns a string, but i is an integer. Try using input instead of raw_input.

7 Comments

input is equivalent to eval(raw_input(prompt)). and eval is evil.. don't do that.
This is only true in Python 3, where they got rid of the raw_input keyword.
This is nonsense - eval is awkward, but it is not evil. There is no security risk here.
Are you kidding?! There's absolutely a security risk here. It's totally possible to produce a string that, when eval'd, downloads and runs a trojan.
@comex It isn't nonsense. Using eval() in a program is a HUGE security risk. One of the most basic principles of computer security is, "All input is evil, unless proven otherwise." You show no respect to this principle when you use eval() and the disrespect to this principle is only magnified when you use eval() with raw_input() because now you not only accept an input that could be evil but you also execute and evaluate it.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.