我正在编写一个简单的测试,该测试应该计算用户回答“是”和“否”的次数。这是我的代码:
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)
我做错了什么?
编辑:我得到的确切输出是:
raflicky@raflicky-MacBookPro:~/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)
答案 0 :(得分:0)
问题是,你有= +而不是+ =。如果将其更改为+ =,则可以使用
答案 1 :(得分:0)
@RafaelMartínez!
您的程序会反转增强的assign运算符。它应该是+=
而不是=+
例如,你的专栏
yes =+ 1
实际上是将1设置为变量yes
,而不是将其先前的值增加为1。
我不知道那是可能的。感谢有趣的例子。