2017-07-22 229 views
-3

我正在編寫一個簡單的測試,應該計算用戶回答「是」和「否」的次數。這裏是我的代碼:計數滿足條件的次數(Python)

questions = ["Are you happy?", "Did you shower?", "Are you Turkish?", "Are you bored?", "Do you have friends?"] 

yes = 0 
no = 0 

for each in questions: 
    answers = input(each) 
    if answers == "yes": 
     yes =+ 1 
    elif answers == "no": 
     no =+ 1 

print(yes, no) 

要麼回答「是」或「否」的問題,回答的Python Output: (0,0)

我在做什麼錯後?

編輯:我得到確切的輸出是在這裏:

[email protected]:~/enviroments$ python quiz.py 
Are you happy?yes 
Did you shower?no 
Are you Turkish?yes 
Are you bored?no 
Do you have friends?yes 
(0, 0) 
+1

目前正在輸出什麼是指示你有什麼不對? – idjaw

+0

我的不好,我會在一秒內編輯它。它給了我'輸出:(0,0)' –

+2

你已經在兩個地方寫了'= +',我相信你會寫'+ ='。 –

回答

0

@RafaelMartínez!

您的程序會顛倒增強的賦值運算符。它應該是+=,而不是=+

比如你行

yes =+ 1 

實際上是設置1到變量yes,在一個沒有增加其先前的值。

我不知道這是可能的。感謝這個有趣的例子。

0

的問題是,你有= +而不是+ =。如果您將其更改爲+ =,它將起作用

+0

它爲我工作 –