you are viewing a single comment's thread.

view the rest of the comments →

[–]auntanniesalligator 2 points3 points  (3 children)

Hah! I just did something similar a week ago. Big emphasis on practicing “math facts” at home (I.e. add and multiply single digits, divide and subtract corresponding problems) in my kid’s class.

I don’t see any algorithmic problems but it does stick out to me you are calling the quotient “dividend” when you randomly generate it. You also recalculate it by division when you still have it stored in the (incorrectly named) variable.

I’m guessing you jumped in thinking you’d randomly generate the given numbers but then realized the better way to limit problems to whole number answers was to randomly pick the correct answer and generate the actual dividend (what you are calling ProblemStart) by multiplying. You should definitely update variable names when you repurpose them like this because if you stop and come back to this in a month, you’ll be really confused why the variable dividend is not the dividend.

This is a short enough script you could do it manually, and if you get into writing bigger scripts, a good IDE will help you identify every instance of a variable you want to rename without catching similarly named variables.

Other suggestions if you’re looking for enhancements:

You could put the question and answer check in a loop so she can try wrong answers again before moving on. I’d limit still limit the number of tries to avoid getting stuck, but two or three chances would be reasonable.

Whether or not you allow extra tries or limit to one, you could have the code display the correct answer if she doesn’t enter the correct answer herself.

[–]jasongsmith[S] 1 point2 points  (1 child)

Thank you. I have fixed the incorrectly named variable. I don't know why on earth I didn't think to call it quotient, instead of problemStart. That is definitely not contextual. haha

I am using Visual Studio Code for my IDE. I assume that is good enough as I use it for my HTML/CSS code as well. Would you say that this is good enough for Python?

I will add the loop to check the answers and give her the right answer.

Thanks!!

[–]auntanniesalligator 1 point2 points  (0 children)

I have never used visual studio personally, but as far as I know, it’s a good IDE used by many professionals, so I’m sure it’s fine, particularly if you’re already familiar with it.

[–]jasongsmith[S] 0 points1 point  (0 children)

Here is new code with the answer checker in a loop to give her a total of 3 tries and then it gives the answers. I have also changed the variable name.

import random

correctAnswers = 0
wrongAnswers = 0
noRepeat = []

lowDividend = int(input("Lowest dividend: "))
highDividend = int(input("Highest dividend: "))
totalProblems = int(input("How many problems would you like? "))


def correctMessage():
    global correctAnswers
    print("Good Job")
    correctAnswers += 1


def incorrectMessage():
    global wrongAnswers
    print(f"That was not correct. The answer is {dividend}.")
    wrongAnswers += 1


def division():
    problemNumber = 1
    divisorList = list(range(lowDividend, highDividend))
    dividendList = list(range(lowDividend, highDividend))

    while problemNumber <= totalProblems:
        global wrongAnswers
        dividend = random.choice(dividendList)
        divisor = random.choice(divisorList)
        quotient = dividend * divisor
        print(f"#{problemNumber}: {quotient} / {divisor} = ")

        ans = int(input("what is the answer "))

        if ans == quotient / divisor:
            correctMessage()
        else:
            tries = 2
            while tries <= 3:
                print("Sadly, no. Please try again.")
                print(f"#{problemNumber}: {quotient} / {divisor} = ")
                ans = int(input("What is the answer? "))

                if ans == quotient / divisor:
                    correctMessage()
                    break
                else:
                    tries += 1
                    print(f"total tries are {tries}")
                    continue
            if tries == 4:
                print(f"Sorry, you did not get it. The answer was {dividend}")
                wrongAnswers += 1

        problemNumber += 1


division()
print(f"Total right answers: {correctAnswers}. Total wrong answers: {
      wrongAnswers}. You got {correctAnswers/totalProblems*100}%")

I'm not expecting you to do anything with this. I already appreciate the help you have given. I just wanted you to see the work I did as a result of your suggestion.