我对编码很新,我正在写一本字典,检查我是否知道某些单词的翻译。到目前为止一直很好,但是我有一个小问题,我确信它很简单,但不知道如何改进代码。
当我得到一个正确的单词时,它应该被添加到变量'right'中,但由于它在while循环中,它只执行一次,当我得到另一个单词时,它不会加起来。我该如何解决这个问题?
for key, value in words:
right = 0
print ('What does', key, 'mean ?')
given_meaning = input ('It means: ')
while True:
if given_meaning == value:
right += 1
x = Decimal(right/dict_len)*100
final_number = round(x, 2)
print ('Yes! You got it right!')
print ('You know', final_number ,'% of the words.')
break
if given_meaning != value:
print ('Unfortunately, this means:', value, '.' )
print ('You know ', final_number ,'% of the words.')
break
它不断显示正确的一个词的结果。 你能帮忙吗?
答案 0 :(得分:0)
right = 0
for key, value in words:
print ('What does', key, 'mean ?')
given_meaning = input ('It means: ')
while True:
if given_meaning == value:
right += 1
x = Decimal(right/dict_len)*100
final_number = round(x, 2)
print ('Yes! You got it right!')
print ('You know', final_number ,'% of the words.')
break
if given_meaning != value:
print ('Unfortunately, this means:', value, '.' )
print ('You know ', final_number ,'% of the words.')
break
这应该可以解决问题。你现在拥有它的方式是,你通过for循环每次迭代都设置right = 0
。