<p><code>if option1 != choice or option1 != choice2</code>对于option1的任何值都是真的。选项2也一样</p>
<p>您可以检查以下内容:</p>
<pre class="lang-py prettyprint-override"><code>option1 = input("Please choose a shape (a/b): ") # User input
if option1 != choice or option1 != choice2:
print(option1 != choice or option1 != choice2) # x or y
# print("x:", option1 != choice)
# print("y:", option1 != choice2)
print("Invalid selection, please only choose the given selection")
</code></pre>
<p>从<a href="https://docs.python.org/3/reference/expressions.html#booleans" rel="nofollow noreferrer">docs</a>:</p>
<blockquote>
<p>The expression <code>x or y</code> first evaluates x; if x is true, its value
is returned; otherwise, y is evaluated and the resulting value is
returned.</p>
</blockquote>
<p><strong>输出案例</strong>:</p>
<p>x为假,y为真</p>
<pre class="lang-sh prettyprint-override"><code>Please choose a shape (a/b): a
True
Invalid selection, please only choose the given selection
</code></pre>
<p>x为真,y为假</p>
<pre class="lang-sh prettyprint-override"><code>Please choose a shape (a/b): b
True
Invalid selection, please only choose the given selection
</code></pre>
<p>x是真的,y是真的</p>
<pre class="lang-sh prettyprint-override"><code>Please choose a shape (a/b): c
True
Invalid selection, please only choose the given selection
</code></pre>
<p><strong>解决方案</strong>:</p>
<p>您可以将有效选项组织到一个列表中</p>
<pre class="lang-py prettyprint-override"><code>choices = ["a", "A", "b", "B"]
</code></pre>
<p>并验证用户输入,如下所示:</p>
<pre class="lang-py prettyprint-override"><code>option1 = input("Please choose a shape (a/b): ") # User input
if option1 not in choices:
print("Invalid selection, please only choose the given selection")
</code></pre>