[英]Why is <= throwing an invalid syntax error in Python
我對 Python 很陌生。 我正在嘗試創建一個循環,在一個范圍之間比較 int。
while counter < N:
x = randn()
if x >= 0 and <=1:
print('0-1')
counter = counter + 1
elif x < 0 and < -1
print("0- -1")
counter = counter + 1
我在<=上不斷收到語法錯誤
File "<ipython-input-35-1d74b6e80ea0>", line 9
if x >= 0 and <=1:
^
語法錯誤:無效語法
對我所缺少的任何幫助將不勝感激
正確的語法是:
if x >= 0 and x <= 1:
你困惑的原因是因為你寫出來就像你向別人解釋一樣。 X 必須大於或等於 0 且小於或等於 1。
然而,在 python 中,這是兩個獨立的條件,需要完整地寫出: x >= 0
和x <= 1
。
或者,您可以選擇將運算符組合成一個條件,如下所示:
if 0 <= x <= 1
以這種方式合並它們會將不等式轉化為單一(復合)條件。
代替
x >= 0 and <=1
經過
x >= 0 and x<=1
你應該試着把它寫成if x >= 0 and x <= 1:
。 and
連接兩個單獨的語句,因此您需要單獨編寫比較。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.