我試圖把JavaScript代碼轉換成python,我遇到的問題是我不知道一些表達如何做,所以現在我卡住了。這是我希望翻譯的代碼片段。這些Javascript表達式(+!)做了什麼?
var generateID = function(dob, male, citizen) {
var gender = getRandom(5) + (male ? 5 : 0);//this line if anyone knows what it does
var citBit = +!citizen;//this is the line that i need to translate to a python equivalent
};
轉換爲布爾再到數。 '!'如果爲'false',則將轉換爲布爾型和一元型'+'爲'0',如果爲'true'則將'1'轉換爲''。 – Tushar
'(男性?5:0)'是[三元操作符](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Operators/Conditional_Operator),基本上,寫一個更小的方法'if' – George
@喬治他問了包含+的第3行!不是關於三元運算符 – Newbie