小红背单词时如果她已经记住了 i 个单词且背了一个没有记住的新单词 i +1次则她就会记住这个新单词。用python编写代码求记住单词的数量
可以使用循环来实现这个过程,代码如下所示:
def count_words():
i = 0
while True:
i += 1
choice = input("Do you remember the new word? (y/n): ")
if choice == 'n':
break
return i
num_words = count_words()
print("The number of words remembered: ", num_words)
运行这段代码后,程序会让用户输入是否记住了新单词,如果用户输入的是'n',则会退出循环,否则会继续循环并增加记住单词的数量。最后程序会输出记住的单词数量。
原文地址: https://www.cveoy.top/t/topic/iInn 著作权归作者所有。请勿转载和采集!