2017-06-25 100 views
0
random_words = ['michael ', 'is', 'cool', 'avyn','tooburt','mooburt','myself','jack','rahim'] 
import random 

i = 0 
while i < 5: 
    score = 0 
    random_word = random.choice(random_words) 
    word = input(random_word + ' ') 
    if random_word == word: 
     score += 1 
    else: 
     score -= 1 
    i += 1 
print('you scored ' + str(score)) 

當我打印比分它總是-1或1,我怎麼記分的軌道正常分數只考慮最後的答案

回答

2

當我打印比分它總是要麼-1或1 [...]

這是因爲score在每個迭代開始時在循環內重新初始化。

外移動這樣的:

score = 0 
while i < 5: 
    ...