我希望打印的值是「200!」和「404!」在Python3.4中。但是我得到了結果「200!」和「別的!」。爲什麼Python 3和C中的if語句的結果不同?
我做了一個奇怪的臉 - 並在C中再次嘗試。C中的結果與Python3中的結果不一樣。爲什麼?
Python3.4代碼
def chk(s):
if s is 200:
print('200!')
elif s is 404:
print('404!')
else:
print('else!')
chk(200)
chk(404)
Python3.4碼結果
200!
else!
C代碼
#include <stdio.h>
void chk(int s) {
if (s == 200)
printf("200!");
else if (s == 404)
printf("404!");
else
printf("else!");
}
int main(void) {
chk(200);
chk(404);
return 0;
}
C代碼結果
200!404!
謝謝。我不使用'is'。 – user10501
@ user10501:實際上,當測試一個名稱是「None」時,你應該明確地使用它。 '== None'肯定是錯誤的方法。 – Olaf
我還想指出'is'對於某些事情是有用的。只是,呃,不是數字。 – NightShadeQueen