• <legend id='ZcBX1'><style id='ZcBX1'><dir id='ZcBX1'><q id='ZcBX1'></q></dir></style></legend>
    <i id='ZcBX1'><tr id='ZcBX1'><dt id='ZcBX1'><q id='ZcBX1'><span id='ZcBX1'><b id='ZcBX1'><form id='ZcBX1'><ins id='ZcBX1'></ins><ul id='ZcBX1'></ul><sub id='ZcBX1'></sub></form><legend id='ZcBX1'></legend><bdo id='ZcBX1'><pre id='ZcBX1'><center id='ZcBX1'></center></pre></bdo></b><th id='ZcBX1'></th></span></q></dt></tr></i><div id='ZcBX1'><tfoot id='ZcBX1'></tfoot><dl id='ZcBX1'><fieldset id='ZcBX1'></fieldset></dl></div>

        <bdo id='ZcBX1'></bdo><ul id='ZcBX1'></ul>
    1. <small id='ZcBX1'></small><noframes id='ZcBX1'>

    2. <tfoot id='ZcBX1'></tfoot>

        while 语句后的 Else 语句,以及字符串与字符串列表的比较?

        时间:2023-08-30

        <small id='SLg1p'></small><noframes id='SLg1p'>

            <i id='SLg1p'><tr id='SLg1p'><dt id='SLg1p'><q id='SLg1p'><span id='SLg1p'><b id='SLg1p'><form id='SLg1p'><ins id='SLg1p'></ins><ul id='SLg1p'></ul><sub id='SLg1p'></sub></form><legend id='SLg1p'></legend><bdo id='SLg1p'><pre id='SLg1p'><center id='SLg1p'></center></pre></bdo></b><th id='SLg1p'></th></span></q></dt></tr></i><div id='SLg1p'><tfoot id='SLg1p'></tfoot><dl id='SLg1p'><fieldset id='SLg1p'></fieldset></dl></div>
            1. <tfoot id='SLg1p'></tfoot>

                  <bdo id='SLg1p'></bdo><ul id='SLg1p'></ul>

                  <legend id='SLg1p'><style id='SLg1p'><dir id='SLg1p'><q id='SLg1p'></q></dir></style></legend>
                    <tbody id='SLg1p'></tbody>
                • 本文介绍了while 语句后的 Else 语句,以及字符串与字符串列表的比较?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我正在尝试创建一个小测试脚本,将某些内容附加到注释中.下面是我将在脚本中执行的主要功能.问题似乎是当 while 块评估为 false 时(也就是说,当它评估为任何不是'不是这四个选项之一),while 块只是在无限循环中继续.我还尝试将 break 插入到 while 循环中,但这会在 while 循环执行后终止脚本.

                  I'm attempting to create a small test script that appends something to a note. Contained below is the main function that I will execute in the script. The problem seems to be that I can't get the else block to run when the while block evaluates to false (that is to say, when it evaluates to anything that isn't one of those four options), the while block just continues in an infinite loop. I've also attempted to insert a break into the while loop but this terminates the script after the while loop has executed.

                  while 块的计算结果为 false 时,如何将其移至 else 块?为什么我现在做事的方式不能像我希望的那样工作?谢谢.

                  How do I move from the while to the else block when it evaluates to false? And why doesn't the current way I do things work as I would like it to? Thanks.

                  def start():
                      q01 = input("What is the subject of your note?
                  ")
                      q02 = input("Are you certain that the subject of your note is " + q01 + "?
                  ")
                      while q02 == 'No' or 'no' or 'NO' or 'n':
                         q01 = input("So, what is the subject of your note?
                  ")
                         q02 = input("Are you certain now that the subject of your note is " + q01 + "?
                  ")
                      else:
                         q03 = Enter("Enter the content of your note")
                  

                  推荐答案

                  你的罪魁祸首是while循环条件:

                  Your culprit is the while loop condition:

                  while q02 == 'No' or 'no' or 'NO' or 'n':
                  

                  这相当于:

                  while (q02 == 'No') or 'no' or 'NO' or 'n':
                  

                  作为 'no''NO''n' 都是非空字符串,它们的计算结果为 True 所以你的条件评估为:

                  As 'no', 'NO' and 'n' are all non-empty strings they evaluate to True and so your condition evaluates to:

                  while (q02 == 'No') or True or True or True:
                  

                  这显然总是True.

                  要解决此问题,您需要将条件调整为:

                  To fix this you need to adjust the condition to:

                  while q02 == 'No' or q02 == 'no' or q02 == 'NO' or q02 == 'n':
                  

                  虽然要更 Pythonic,但您可以改为:

                  Although to be more pythonic you could instead make this:

                  while q02 in ['No','no','NO','n']:
                  

                  这篇关于while 语句后的 Else 语句,以及字符串与字符串列表的比较?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:Python - 作为条件的模运算 下一篇:在 Pandas DataFrame 中评估 if-then-else 块中的多个条件

                  相关文章

                  1. <tfoot id='yHUwT'></tfoot>

                      <bdo id='yHUwT'></bdo><ul id='yHUwT'></ul>
                    <legend id='yHUwT'><style id='yHUwT'><dir id='yHUwT'><q id='yHUwT'></q></dir></style></legend>

                      <i id='yHUwT'><tr id='yHUwT'><dt id='yHUwT'><q id='yHUwT'><span id='yHUwT'><b id='yHUwT'><form id='yHUwT'><ins id='yHUwT'></ins><ul id='yHUwT'></ul><sub id='yHUwT'></sub></form><legend id='yHUwT'></legend><bdo id='yHUwT'><pre id='yHUwT'><center id='yHUwT'></center></pre></bdo></b><th id='yHUwT'></th></span></q></dt></tr></i><div id='yHUwT'><tfoot id='yHUwT'></tfoot><dl id='yHUwT'><fieldset id='yHUwT'></fieldset></dl></div>
                    1. <small id='yHUwT'></small><noframes id='yHUwT'>