嵌套 While 循环不起作用
我正在尝试使用 while 循环来查找两个值之间的匹配项。一个是静态的,而另一个是列表中的条目。这是代码:
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
实际上,哈希确实等于 hashlist[247],但 python 没有识别它并使用 print wordlist[247] 继续代码,而是挂在嵌套的 While 循环中。有什么想法或建议吗?
谢谢!
编辑:修复了缩进并删除了不相关的变量。
编辑#2:所有变量均在脚本的前面定义。这只是给我带来麻烦的一小段代码。 Hash 和 Hashlist[247] 相等(print hash 和 print hashlist[247] 各自给出 848 作为输出)。
编辑#3:已解决 - 感谢您的帮助!
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[247], 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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您发布的代码不嵌套任何 while 循环。
这是唯一相关的代码。这是一个无限循环,假设计数不是从 10 开始。
The code you posted doesn't nest any while loops.
This is the only relevant code. This is an infinite loop assuming count didn't start at 10.
事情 1: 执行 10 次某事的 Python 方式是
事情 2: 显然 Python 认为
hash != hashlist[247]
,或者它不会无限循环。尝试 print hash, hashlist[247], hash == hashlist[247] 进行检查。事情 3:
while cond: pass
的意义何在?您是否正在尝试做多线程之类的事情?Thing 1: the Pythonic way of doing something 10 times is
Thing 2: clearly Python thinks that
hash != hashlist[247]
, or it wouldn't loop infinitely. Tryprint 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?考虑更新后的帖子(带有缩进代码):如果
count
的初始值大于10
,则顶级while
将是无限的。 。另外,如果
hash != hashlist[247]
,则以下循环也将是无限的(如果没有自定义__getitem__, __eq__
并从另一个线程更改值):Considering the updated post (with indented code): the top-level
while
will be infinite, if the initial value ofcount
is greater than10
.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):这是由于哈希和哈希列表的类型不同:/。字符串和整数。我忽略了这一点,因为 python 解释器没有提到任何有关类型错误的内容,而我已经习惯了这样做,而且我只是忘了检查。
感谢大家的帮助!
对于任何有类似问题的人:
请仔细检查您的类型!
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!!!