1

Is there a way to repeat a single function in a while loop one in every 5 times the loop runs? I am trying to create a bot to help me with my Latin revision but I don't want the option to close the program to crop up every time I answer a question, it would be better if it only happened 1 out of 10 times.

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)")
cdlane
  • 40,441
  • 5
  • 32
  • 81

1 Answers1

1

To solve your issue, we can add a question counter and use the modulus operator (%) to trigger the exit option on every fifth question.

However, there are some other problems to address. For example, this:

,"dinner" or "meal",

is just wishful thinking -- it doesn't work that way. We can turn this into a list of possible answers. Next, whenever we have parallel arrays like noun1L and noun1E, it usually means we're missing a data structure. Finally, don't store the data in the code, separate them.

Here's my rework of your code addressing the above issues:

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
cdlane
  • 40,441
  • 5
  • 32
  • 81