Step-by-step explanation:
For this program we'll need to know a couple concepts which are: while loops, user input, variables, converting strings to integers, and basic arithmetic operators, if statements, and the random module.
So let's first just declare the basic function, we can call this "main", or something a bit more descriptive, but for now I'll name it "main".
def main():
# code will go here
main()
so now from here, we want to initialize some variables. We need to somehow keep track of how many they get correct and how many they've answered.
These two numbers may be the same, but at times they will be different from each other, so we'll need two variables. Let's just call the variable tracking how much have been answered as "answered" and the variable tracking how much are correct as "correct".
These two will initially be zero, since the user hasn't answered any questions or gotten any correct. So now we have the following code
def main():
correct = 0
answered = 0
main()
Now let's use a while loop, which we break out of, once the user inputs zero. This may be a bit tricky if we use a condition since we'll have to ask for input before, so let's just use a while True loop, and then use an if statement to break out of it at the end of the loop.
Now we want to generate two numbers so we can use the random module. To access the functions from the random module you use the import statement which will look like this "import random". Now that you have access to these functions, we can use the randint function which generates random numbers between the two parameters you give it (including those end points). It says two digits, so let's use the endpoints 10 and 98, and I'll explain later why I'm limiting it to 98 and not 99.
The reason we want to limit it to 98 and not 99, is because it's possible for the two randomly generated numbers to be equal to each other, so the answer would be zero. This is a problem because the zero is used to quit the program. So what we can do in this case, is add one to one of the numbers, so they're no longer equal, but if they're equal to 99, then now we have a three digit number.
Now onto the user input for simplicitly, let's assume they enter valid input, all we have to do is store that input in a variable and convert it into an integer. We can immediately convert the input into an integer by surrounding the input by the int to convert it.
we of course want to display them the equation, and we can either do this through string concatenation or f-strings, but f-strings are a bit more easier to read.
So let's code this up:
import random
def main():
correct = 0
answered = 0
while True:
num1 = random.randint(10, 98)
num2 = random.randint(10, 98)
if num1 == num2:
num2 += 1
userInput = int(input(f"{num1} - {num2}"))
main()
from here we first need to check if they entered zero and if so, break out of the loop. If they didn't enter zero, check if the userInput is equal to the actual answer and if it is, then add one to correct and finally add one to answered regardless of whether their answer is correct or not.
Outside the loop to display how much they got correct we can use an f-string just like we did previously. Since sometimes we'll get a non-terminating decimal, we can use the round function so it rounds to the nearest hundreth.
So let's code this up:
import random
def main():
correct = 0
answered = 0
while True:
num1 = random.randint(10, 98)
num2 = random.randint(10, 98)
if num1 == num2: # the answer would be zero
num2 += 1 # makes sure the answer isn't zero
userInput = int(input(f"{num1} - {num2}"))
if userInput == 0: # first check if they want to stop
break
if userInput == (num1 - num2):
correct += 1
answered += 1
print(f"Correct: {correct}\\Incorrect: {answered - correct}\\Percent: {round(correct/answered, 2)}")
main()
and that should pretty much be it. The last line is just some formatting so it looks a bit better when displaying.