user1676480
user1676480

Reputation: 43

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()

Upvotes: 4

Views: 14170

Answers (8)

Gojko Kojic
Gojko Kojic

Reputation: 1

Write a multiplication game program for kids. The program should give the player ten randomly generated multiplication questions to do. After each, the program should tell them whether they got it right or wrong and what the correct answer is

from random import randint 

for i in range (1,11):
    num1 = randint (1,10)
    num2 = randint (1,10)
    print ("Question",i,":",num1,"*",num2,"=", end = " ")
    guess = int (input())
    answer = num1*num2
    if guess == answer:
        print ("Right!")
    else:
        print ("Wrong. The answer is: ",answer)

Upvotes: 0

Pierre GM
Pierre GM

Reputation: 20349

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 use gamenumber as an argument of main, 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 as correct/wrong. While I'm sure you meant correct/gamenumber, you have to be extra careful: count and gamenumber are integers, and dividing integers is no the same as dividing floats. For example, 2/3 gives 0, but 2/float(3) gives 0.6666666. So, we'll have to use a float somewhere.
  • You want to print a percentage: your result should then be result=correct*100./gamenumber.
  • You don't want to gamenumber to be 0, otherwise your result 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.")

Upvotes: 2

John La Rooy
John La Rooy

Reputation: 304393

import random
import math

spelling of "problems" is wrong

gamenumber = int(input("How many probems do you want?\n"))

move these next two lines inside the loop

num_1 = random.randint(1,10)
num_2 = random.randint(1,10)

def main():

    random.seed()
    count = 0
    while count < gamenumber:

You can use "What is {}x{}?".format(num1, num2) here.

        guess = int(input("What is " + str(num_1) + "x" + str(num_2) + "."))

        answer = str(num_1*num_2)

Is this supposed to count the correct answers? should be correct += guess == answer

       correct = guess == answer

Do you mean to count the number of wrong answers? wrong += guess != answer

        if guess == answer:
            print("Correct!")

else wrong: is a syntax error else: #wrong perhaps?

        else wrong:
            print("Sorry, the answer is", answer, ".")

This isn't how to compute a percentage. You should use correct*100/gamenumber and dedent to match the print()

        result = correct/wrong   

    print("You got ", "%.1f"%result, "of the problems.")

main()

Also you're not incrementing count anywhere. It's easier to just use

for count in range(gamenumber):

instead of the while loop

Upvotes: 1

JasonDAquila
JasonDAquila

Reputation: 88

Python is a procedural language. It executes statements in your method body from top to bottom. This line:

num_1 = random.randint(1,10)

is an assignment statement. It does not equate num_1 with a random process for assessing its value; it evaluates an expression - by calling random.randint(1,10) - and assigns that value to num_1, once.

You must force another call to random.randint to obtain another random number, and you must have a statement assign a value to num_1 each time you want num_1's value to change.

Upvotes: 0

filip-fku
filip-fku

Reputation: 3265

  1. You're only generating the question numbers once, before you start looping. You need to generate num_1 and num_2 every time, before the user is asked a question.

  2. You never actually update the count value after initializing it, so your loop will go on forever.

Upvotes: 1

Borgleader
Borgleader

Reputation: 15916

You forgot to increment count in your loop and num_1 and num_2 don't get new values.

Upvotes: 4

Makoto
Makoto

Reputation: 106480

The most glaring issue to me is that you have an infinite loop; you don't increase count anywhere.

Upvotes: 1

asthasr
asthasr

Reputation: 9417

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.

Upvotes: 4

Related Questions