2016-11-24 93 views
-2

我嘗試添加根據用戶的回答一個變量提供如添加到一個變量在python

points = 0 
print("Do you have a cat or a dog?") 
response = input() 
if response == 'Dog' points=+1 
elif response == 'Cat' points=+2 

「點」是變量,它是0,現在這樣依賴於答案用戶給我如何添加一個數字變量,以便它可以從0更改爲1或2

+0

使用字典動物名稱與關聯點數。 –

回答

-1
points = 0 
print("Do you have a cat or a dog?") 
response = input() 
if response == 'Dog': points=+1 
elif response == 'Cat': points=+2 

print points 

試試這個代碼,你的if語句沒有

+0

該問題詢問如何將_添加到變量中;此解決方案不使用添加。 –

+0

@BryanOakley給我舉個例子 – Anthony

1
points = 0 
print("Do you have a cat or a dog?") 
response = input() 
if response == 'Dog' :points+=1 
elif response == 'Cat':points+=2 

我認爲你必須把+ =而不是= +。

或者,也許,這是simplier

points=points+1 
0
points = 0 
print("Do you have a cat or a dog?") 
response = input() 
if response == 'Dog' points=+1 
elif response == 'Cat' points=+2 

計算機認爲這是

points = 0 
print("Do you have a cat or a dog?") 
response = input() 
if response == 'Dog' 0=+1 
elif response == 'Cat' 0=+2 

因爲點= 0,所以當你 「添加」 到它, 點= + 1 你正在增加一個到沒有和不影響分值。 這樣做的更好的方式是 點=點+ 1 和電腦認爲,隨着 0 = 0 + 1 或 點+ = 1 ,使其更容易