I am supposed to write a program in python that asks the user how many multiplication questions they want, and it randomly gives them questions with values from 1 to 10. Then it spits out the percentage they got correct. My code keeps repeating the same set of numbers and it also doesn’t stop at the number the user asked for. Could you tell me what’s wrong?
import random import math gamenumber = int(input("How many probems do you want?n")) num_1 = random.randint(1,10) num_2 = random.randint(1,10) def main(): random.seed() count = 0 while count < gamenumber: guess = int(input("What is " + str(num_1) + "x" + str(num_2) + ".")) answer = str(num_1*num_2) correct = guess == answer if guess == answer: print("Correct!") else wrong: print("Sorry, the answer is", answer, ".") result = correct/wrong print("You got ", "%.1f"%result, "of the problems.") main()
Advertisement
Answer
Problems you mentioned
My code keeps repeating the same set of numbers
This is no surprise, as you set your num_1
and num_2
(1) outside the main
function and (2) outside the main while
loop. A simple correction is:
while count < gamenumber: num_1 = random.randint(1,10) num_2 = random.randint(1,10)
My code doens’t stop at the number asked for:
There again, no surprise, as you never increment the count
counter: you always have count < gamenumber
.
A simple correction is:
while count < gamenumber: num_1 = random.randint(1,10) num_2 = random.randint(1,10) guess = int(input("What is " + str(num_1) + "x" + str(num_2) + ".")) answer = str(num_1*num_2) count += 1
Here, the count += 1
means add 1 to count *in place*
. You could also do count = count + 1
but it’s a bit less efficient as you create a temporary variable (count + 1
) that you don’t really need.
Other problems
- You never define
wrong
- You define
gamenumber
outside the function. While it’s not an issue in this case, it’d be easier to usegamenumber
as an argument ofmain
, as it’s the variable that drives the game. - Your
result
is defined in the loop. You probably want to increment a counter for each good answer and print the result after the main loop. - Your
result
is calculated ascorrect/wrong
. While I’m sure you meantcorrect/gamenumber
, you have to be extra careful:count
andgamenumber
are integers, and dividing integers is no the same as dividing floats. For example,2/3
gives0
, but2/float(3)
gives0.6666666
. So, we’ll have to use a float somewhere. - You want to print a percentage: your
result
should then beresult=correct*100./gamenumber
. - You don’t want to
gamenumber
to be 0, otherwise yourresult
will be undefined.
So, all in all, your main
function should be
def main(gamenumber): random.seed() count = 0 correct = 0 while count < gamenumber: num_1 = random.randint(1,10) num_2 = random.randint(1,10) guess = int(input("What is " + str(num_1) + "x" + str(num_2) + ".")) answer = str(num_1*num_2) count += 1 if guess == answer: correct += 1 print("Correct!") else wrong: print("Sorry, the answer is", answer, ".") if gamenumber > 1: result = correct * 100./gamenumber print("You got ", "%.1f"%result, "of the problems.")