嵌套 While 循环不起作用

发布于 2024-10-07 23:41:31 字数 637 浏览 0 评论 0原文

我正在尝试使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

怪异←思 2024-10-14 23:41:31

您发布的代码不嵌套任何 while 循环。

while count != 10:

    for x in rawinput[pos]:
       a = ord(x)
       hash = hash + a

这是唯一相关的代码。这是一个无限循环,假设计数不是从 10 开始。

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.

猛虎独行 2024-10-14 23:41:31

事情 1: 执行 10 次某事的 Python 方式是

for _ in range(10):
    ...

事情 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

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?

痴梦一场 2024-10-14 23:41:31

考虑更新后的帖子(带有缩进代码):如果 count 的初始值大于 10,则顶级 while 将是无限的。 。

另外,如果 hash != hashlist[247],则以下循环也将是无限的(如果没有自定义 __getitem__, __eq__ 并从另一个线程更改值):

...
while hash != hashlist[247]:
   pass
...

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
...
哀由 2024-10-14 23:41:31

这是由于哈希和哈希列表的类型不同:/。字符串和整数。我忽略了这一点,因为 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!!!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文