簡體   English   中英

在Python中的If語句中使用布爾值

[英]Using a Boolean in an If-Statement in Python

我有一些帶有if語句的代碼,其中一個條件是布爾值。 但是,CodeSkulptor說“第36行:TypeError:BitAnd不支持的操作數類型:'bool'和'number'”。 如果可以的話請幫忙。 這就是那段代碼的樣子。 (我只是更改了所有變量名稱以及if語句執行的內容)

thing1 = True
thing2 = 3

if thing2 == 3 & thing1:
   print "hi"

你想使用邏輯and (而不是& ,這是Python中的按位AND運算符):

if thing2 == 3 and thing1:
   print "hi"

因為你使用了& ,所以出現了錯誤,說:

TypeError: unsupported operand type(s) for BitAnd: 'bool' and 'number'
                                           ^^^^^^

&是按位AND運算符。 你想使用邏輯and來代替:

if thing2 == 3 and thing1:
    print "hi"

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM