2017-06-12 59 views
0

我試圖把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 
    }; 
+3

轉換爲布爾再到數。 '!'如果爲'false',則將轉換爲布爾型和一元型'+'爲'0',如果爲'true'則將'1'轉換爲''。 – Tushar

+0

'(男性?5:0)'是[三元操作符](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Operators/Conditional_Operator),基本上,寫一個更小的方法'if' – George

+0

@喬治他問了包含+的第3行!不是關於三元運算符 – Newbie

回答

0

它會轉換成布爾

getRandom = function(s) { return s } 
 

 
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 
 
     console.log(citBit); 
 
    }; 
 
    
 
generateID(null , 'yes', 1)

+0

謝謝,這有幫助! – user2479669

+0

如果你覺得接受我的回答支持 – Newbie

0
+! will test return opposite boolean representation for your input. 

if your input is `true` will return false. 
if it is `false` will return `true` 

爲如:

你給定的輸入是> 0其布爾表示是1所以它會做!並返回false等效0。同樣,如果0將返回1

+0

謝謝穆罕默德! – user2479669

0

返回true如果值是falsefalse如果該值是true