簡體   English   中英

'str'和'str'不支持的操作數類型。 蟒蛇

[英]Unsupported operand type(s) for 'str' and 'str'. Python

我有IF聲明;

if contactstring == "['Practice Address Not Available']" | contactstring == "['']":

我不確定出了什么問題(可能是“”的?),但我一直收到標題中提到的錯誤。

我在其他問題中尋找答案,但是所有這些似乎都是關於對字符串使用數學運算的,這里不是這種情況。 我知道這個問題有點懶惰,但是我整天都在編寫代碼,而且筋疲力盡,我只是想盡快解決這個問題。(Python newb)

| 是Python中的按位運算符或運算符,並且具有優先級,因此Python可以將其解析為:

if contactstring == (""['Practice Address Not Available']"" | contactstring) == "['']":

這會產生您看到的錯誤。

似乎您想要的是邏輯或運算符,在Python中拼寫為“或”:

if contactstring == ""['Practice Address Not Available']"" or contactstring == "['']":

會做您期望的。 但是,由於您要將同一變量與一系列值進行比較,因此更好:

 if contactstring in ("['Practice Address Not Available']", ['']):

| 按位運算 ,不適用於字符串...

使用or (布爾邏輯運算符)將產生更好的結果。

這里的問題是按位或運算符| 在通常可以正常工作的布爾上下文中,但是| 具有比==更高的優先級,因此Python嘗試評估"['Practice Address Not Available']" | contactstring "['Practice Address Not Available']" | contactstring首先。 這兩個操作數都是字符串,並且不能按位或兩個字符串。 使用更正確的方法or避免此問題,因為它的優先級低於==

暫無
暫無

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

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