![](/img/trans.png)
[英]How can I get my function to return more than 1 result (with for-loops and dictionaries) in Python?
[英]How do I use Python for-loops to get my program to return the right values?
我只是在練習/測試 for 循環,想知道為什么這段代碼不會返回正確的值。
a = 'abcdefghijklm'
b = 'nopqrstuvwxyz'
word = input('Give a word\n')
score = 0
for letter in word:
if letter in a:
score += score + 1
else:
score += score + 2
print(score)
要將score
增加1
,您應該score += 1
或score = score + 1
。
那是因為您正在使用 += 運算符。
您應該使用score+= 1
和score+=2
或score= score+1
和score= score+2
score += score + 1
,這是不對的score += 1
或score = score + 1
您究竟期待什么 output? 乍一看,如果您想將 score 增加 1,也許您應該使用score += 1
而不是score += score + 1
1。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.