-2

I try to make this code for my little one to practice simple mathematics and I want to give to the user 3 problems to solve, but the program runs random times of problems. What is it I have done wrong? Thanks

import random

for i in range(3):             # number of problems          

    for n in range(999):
        num1 = random.randrange(11)
        num2 = random.randrange(11)
        if num1<num2:
            break
        else:
            sum = eval(input("how much is {} - {} = ".format(num1,num2)))
            if sum == num1 - num2:
                print ("Good job!")
                break      

        for x in range(5):           #nr of wrong tries     
            if sum != num1 - num2:
                sum = eval(input("Try again {} - {} = ".format(num1,num2)))
            else:
                print ("Good job!")
                break
Klod
  • 7
  • 1
  • 1
    You didn't explain what your question is. What do you want your program to do, and what is it doing instead? – BrenBarn Jan 30 '17 at 19:17
  • HI , Like I said , I want the program to give 3 different problems to the user like this: how much is 2-2= how much is 3-2= how much is 6-1= but it gives 2 or 1 or 0 problems and some times 3 problems as well – Klod Jan 30 '17 at 19:21
  • 1
    If `num1 < num2` is true you'll break out of the inner for loop, so it'll run 3 or less questions. Use `continue` instead of `break`, or even better, use a `while` loop. `while` loops are used for looping until a condition is met while `for` loops are used when you know how many times you want to loop. – Ted Klein Bergman Jan 30 '17 at 19:24

3 Answers3

1

The following should work

import random

for i in range(3):             # number of problems          
    num1 = random.randrange(11)
    num2 = random.randrange(11)

    while num1<num2:
        num1 = random.randrange(11)
        num2 = random.randrange(11)

    sum = input("how much is {} - {} = ".format(num1,num2))

    for x in range(5):           #nr of wrong tries     
        if sum != num1 - num2:
            sum = input("Try again {} - {} = ".format(num1,num2))
        else:
            print ("Good job!")
            break

I just replaced an unwanted for loop and if statement with a while.

Jithin Pavithran
  • 1,250
  • 2
  • 16
  • 41
0

Your main problem is the structure of your program, it's confusing, which is why you're here, and error prone, also why you're here.

Try something like this pseudocode:

loop three times
  num1 = -1
  num2 = 0
  while(num1 < num2)
    assign random numbers to num1 and num2
  end loop

  input = ask user what num2 - num1 is

  if input == num2 - num1
    print 'yay'
  else
    print 'boo'
  end if

end loop

This cleans things up and gets rid of those breaks.

Ray Stojonic
  • 1,260
  • 1
  • 7
  • 11
0

If num1 < num2 is true you'll break out of the inner for loop, so it'll run 3 questions or less, depending on how many times num1 < num2 is True. Quickest fix would be to change the break to continue inside that if block.

However, it's better to use a while loop when you don't know how many times you want to loop, and only want to stop when a certain condition is met. In your case you want to loop until num1 > num2, and then ask the question.

An even better solution would be to generate a random number num1 between 1-10 and then generate a random number num2 between 0-num1, which always will make sure num1 is greater than num2.

Last notes:

  • Use int to convert the input to an integer instead of eval.
  • sum is a built-in function so use another name so you don't shadow it.

Here's a functioning example:

import random

for i in range(3):
    num1 = random.randrange(1, 11)  # Has to be greater than 0 so num2 can be less.
    num2 = random.randrange(num1)

    # Use 'int' to convert to int, not 'eval'!
    # Also, 'sum' is a built-in function. Don't shadow it, use another name.
    result = int(input("how much is {} - {} = ".format(num1, num2)))  

    for x in range(5):  # nr of wrong tries     
        if result != num1 - num2:
            result = int(input("Try again {} - {} = ".format(num1, num2)))
        else:
            print("Good job!")
            break

You can expand on this by adding a try-catch block to makes sure that the input really is an number. The program will crash if you input anything else than digits. Here is how you can validate the input.

Community
  • 1
  • 1
Ted Klein Bergman
  • 9,146
  • 4
  • 29
  • 50
  • Thanks all for your replies Ted Klein Bergman yours is spot on , very informative and it works as I intended thanks – Klod Jan 30 '17 at 20:00