views:

162

answers:

5

I believe the word is "recurse" instead of 'start over.' I've created this program to hone my multiplication skills in the morning. I can get it to give me a multiplication problem, but how do I get it to ask me another one?

from random import randint

print 'Good Morning Pete!'

X = randint(0, 10)

Y = randint(0, 10)

A = X * Y

Z = int(raw_input('%i * %i = ? ' % (X, Y)))

count = 0

if Z == A:
    count += 1
    print 'Good Job!'
else:
    print 'Sorry!'


if count == '10':
    print 'Time to kill \'em'

how do I get it to spit out a new problem for me to solve? I'm a beginner. Thanks all!

+1  A: 

A loop? See the for statement and the range() function. They're in the Python tutorial.

And you might want to read the next chapter in whatever book you're using to teach yourself programming.

Chris B.
+3  A: 

Pete, you wouldn't need recursion in this case, but merely a loop.

I suggest you put the bulk of the logic of this program (the part that asks the multiplcation problem and check your answer), into a function. Say One Problem(). This function could return 0 if you answered wrong, 1 if you answered correctly and -1 if you entered some key indicating that you want to stop. (BTW, this function is introduced to help you structure the program, make it more readeable but it is not needed for introducing a loop. you could well keep all this stuff inside the loop. Also, you should know that there are other loop constructs in python, for exampe while loops.)

Then you'd just need in your main section something like that :

GoodReplyCtr = 0
for i in range(0, 10):  # or 100 or 1000 if you feel ambitious...
   cc = OneProblem()
   if cc < 0:
        break
   GoodReplyCtr += cc

print(GoodReplyCtr)

The concept of recursion (again not needed here), is when a function calls itself. This is a common practice when navigating graphs (like say the directory structure on you drive C:), or with some mathematical problems. We typically do not need to cover recursion early in the learning of computer languages concepts, but once you have a good mastery of things, you may find it quite useful (and challenging at time ;-) )

Keep at it! Math and python are cool.

Edit: One last trick:

You may find that you need to work on some multiplication tables more than other. Rather than using randint you can use the random's module random.choice() method to favor some numbers or to eliminate others. for example

import random
X = random.choice((2, 3, 4, 6, 7, 8, 9, 7, 9))  # see, no 0, 1,or 5 but more chance to get 7 or 9
mjv
thanks for introducing the idea of making a function. Nice answer too!
Pete
A: 

to incorporate the loop, you might add this to the beginning of your code:

running = True while True: //Add your code here

//Add this to the end of your code:

print 'Another problem? Enter y or n'
    answer = raw_input().lower()
    if answer == 'n':
        running = False
        break
    elif answer == 'y':
        running = True


this will allow the user to choose if they want another problem each time.

samfu_1
A: 

I agree 100% with those who said this isn't a good case for recursion, but calls for a loop instead. However, for the sake of showing how it might be done, I post the code below:

import random
def do_mult(num_questions):
    x = random.randint(0, 10)
    y = random.randint(0, 10)
    a = x * y
    z = int(raw_input('%i * %i = ?' % (x, y)))
    if z == a:
        print 'good job!'
    else:
        print 'sorry!'
    if num_questions > 1:
        do_mult(num_questions - 1)

do_mult(10)
PTBNL
+1  A: 

I believe you want something like this

from random import randint

print 'Good Morning Pete!'

count = 0

while True:
    X = randint(0, 10)
    Y = randint(0, 10)
    A = X * Y
    Z = int(raw_input('%i * %i = ? ' % (X, Y)))

    if Z == A:
        count += 1
        print 'Good Job!'
    else:
        print 'Sorry!'


    if count == 10:
        print 'Time to kill \'em'
        break
Unknown