Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

multiplication game python

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()
like image 666
user1676480 Avatar asked Dec 11 '22 22:12

user1676480


1 Answers

You only assign to num_1 and num_2 once. Their values never change; how can your numbers change? Furthermore, you don't increment count, so its original value is always compared against gamenumber.

You need to assign a new random number to your two variables and increment your counter.

like image 177
asthasr Avatar answered Jan 13 '23 22:01

asthasr