Python 2.7 循环运行时,每5次重复一个函数
在循环运行的每5次循环一次时,是否有方法在Python 2.7 循环运行时,每5次重复一个函数,python-2.7,latin,Python 2.7,Latin,在循环运行的每5次循环一次时,是否有方法在中重复一个函数?我正在尝试创建一个机器人来帮助我修改拉丁语,但我不希望每次我回答问题时都突然出现关闭程序的选项,如果它只发生10次中的1次会更好 import random exit = "no" print "welcome to latin learner v1" wordtype = raw_input("what would you like to learn (nouns verbs everything)") if wordtype == "
中重复一个函数?我正在尝试创建一个机器人来帮助我修改拉丁语,但我不希望每次我回答问题时都突然出现关闭程序的选项,如果它只发生10次中的1次会更好
import random
exit = "no"
print "welcome to latin learner v1"
wordtype = raw_input("what would you like to learn (nouns verbs everything)")
if wordtype == "nouns":
declension = raw_input("declension 1-5")
if declension == "1":
while "no" in exit:
wordno = random.randint(1,30)
noun1L = ["ancilla","aqua","cena","copiae","cura","dea","domina","epistula","femina","filia","hora","ianua","insula","ira","nauta","patria","pecunia","poena","porta","puella","regina","Roma","silva","taberna","terra","turba","via","victoria","villa","vita"]
answer = raw_input(noun1L[wordno])
noun1E = ["slave-girl" or"slave-woman","water","dinner" or "meal","forces" or "troops","care" or "worry","goddess","mistress","letter","woman","daughter","hour","door","island" or "block of flats","anger","sailor","country" or "homeland","money","punishment","gate","girl","queen","Rome","wood","shop" or "inn","ground" or "land" or "country","crowd","street" or "road" or "way","victory","house" or "country villa","life"]
if noun1E[wordno] == answer:
print "correct"
else:
print "incorrect"
print noun1E[wordno]
for i in range[1,5]:
exit = raw_input("would you like to quit (yes/no)")
为了解决您的问题,我们可以添加一个问题计数器,并使用模数运算符(%)在每五个问题上触发退出选项
然而,还有一些其他问题需要解决。例如,这:
,"dinner" or "meal",
这只是一厢情愿——不是那样的。我们可以把它变成一系列可能的答案。接下来,每当我们有类似于noun1L
和noun1E
的并行数组时,通常意味着我们缺少一个数据结构。最后,不要将数据存储在代码中,而是将它们分开
以下是我对解决上述问题的代码的修改:
import random
noun1 = {
"ancilla": ["slave-girl", "slave-woman"],
"aqua": ["water"],
"cena": ["dinner", "meal"],
"copiae": ["forces", "troops"],
"cura": ["care", "worry"],
"dea": ["goddess"],
"domina": ["mistress"],
"epistula": ["letter"],
"femina": ["woman"],
"filia": ["daughter"],
"hora": ["hour"],
"ianua": ["door"],
"insula": ["island", "block of flats"],
"ira": ["anger"],
"nauta": ["sailor"],
"patria": ["country", "homeland"],
"pecunia": ["money"],
"poena": ["punishment"],
"porta": ["gate"],
"puella": ["girl"],
"regina": ["queen"],
"Roma": ["Rome"],
"silva": ["wood"],
"taberna": ["shop", "inn"],
"terra": ["ground", "land", "country"],
"turba": ["crowd"],
"via": ["street", "road", "way"],
"victoria": ["victory"],
"villa": ["house", "country villa"],
"vita": ["life"],
}
print("Welcome to Latin Learner v1")
wordtype = raw_input("What would you like to learn (nouns verbs everything): ")
if wordtype == "nouns" or wordtype == "everything":
declension = raw_input("Declension 1-5: ")
if declension == "1":
count = 1
while True:
word = random.choice(list(noun1))
answer = raw_input(word +": ")
if answer.lower() in noun1[word]:
print("Correct.")
else:
print("Incorrect: " + ", ".join(noun1[word]))
if count % 5 == 0:
answer = raw_input("would you like to quit (yes/no): ")
if "y" in answer.lower():
break
count += 1