I'm trying to use a while-loop to find a match between two values. One is static while the other is an entry in a list. This is the code:
while count != 10:
for x in rawinput[pos]:
a = ord(x)
hash = hash + a
print hashlist[247]
print hash
print wordlist[247]
while hash != hashlist[247]:
pass
print wordlist[247]
hash = 0
count = count + 1
In reality, hash DOES equal hashlist[24开发者_如何学运维7], but instead of recognizing it and continuing the code with print wordlist[247], python gets hung up at the nested While loop. Any ideas or suggestions?
Thanks!
Edit: Fixed Indentation and removed non-relevant variables.
Edit #2: All variables are defined earlier in the script. This is only a snippet of code that is giving me trouble. Hash and Hashlist[247] are equal (print hash and print hashlist[247] each give 848 as output).
Edit #3: SOLVED -- Thanks for the help!
The code you posted doesn't nest any while loops.
while count != 10:
for x in rawinput[pos]:
a = ord(x)
hash = hash + a
This is the only relevant code. This is an infinite loop assuming count didn't start at 10.
Thing 1: the Pythonic way of doing something 10 times is
for _ in range(10):
...
Thing 2: clearly Python thinks that hash != hashlist[247]
, or it wouldn't loop infinitely. Try print hash, hashlist[247], hash == hashlist[247]
to check.
Thing 3: what's the point of while cond: pass
anyway? Are you trying to do multithreaded stuff or something?
Considering the updated post (with indented code): the top-level while
will be infinite, if the initial value of count
is greater than 10
.
Also, if hash != hashlist[247]
, the following loop will be infinite as well (if there are no custom __getitem__, __eq__
and changing values from another thread):
...
while hash != hashlist[247]:
pass
...
This was due to hash and hashlist being of a different type :/. str and int. I overlooked this, since the python interpreter didn't mention anything about a typeerror, which I'm used to it doing, and I simply forgot to check.
Thanks to everyone for your help!
To anyone who has a similar problem:
DOUBLE CHECK YOUR TYPES!!!
精彩评论