1

Newbie in programming and need something like this but it doesn't stop the while condition even after entering a string starting with '0x', and it keeps calling the function.I tired putting 'break' instead of increasing i value, still didn't work. Any one knows why?

i = 1
while i < 2:
    call_fun1()
    if hx =='0x':
        i+=1
4

2 回答 2

1

The above code will run into an infinite loop only when the while condition

while i < 2:

always turns out to be true. This will happen when the code section that is changing the value of i, i.e.

i+=1

is never executed. This will happen when the if condition

if hx =='0x':

always turns out to be false.

So check why

if hx =='0x':

is always evaluated to false. This should fix the problem.

于 2013-09-07T20:42:01.563 回答
0

With if hx =='0x', you are checking if hx equals "0x", not if it starts with it. You need to find a 'substring'. Check this question: Examples for string find in Python

Because of this, i is never incremented and therefore the loop will continue indefinitely.

于 2013-09-07T20:40:46.153 回答