I made this code:

import random
wish = "j"
max_multiplications = 10
while wish == "j":
    multi = 0
    right_answer = 0
    while max_multiplications != multi:

        randomint1 = random.randint(1, 10)
        randomint2 = random.randint(1, 10)

        print randomint1, "*", randomint2, "="
        ik = float(raw_input ("Insert your answer "))                  
        multi += 1                   
        answer = randomint1 * randomint2
        
        if ik == answer:
            right_answer += 1
            print "Correct!Good Job!"                                  
        else:
            print "Wrong answer!"
                             
   
    else:
        wish = raw_input("You want to try again? [j/e]")

But maybe someone can help me now. How can I put into the beginning ,that the user can choose how many multiplications he wants to solve(not 10) AND if the user inserts a wrong answer he can try again.

Thanks! :)

Recommended Answers

All 10 Replies

You can try:

while True:
    user = raw_input("How many multiplications ? ")
    try:
        max_multiplications = int(user)
        if max_multiplications <= 0:
            raise ValueError
        else:
            break
    except ValueError:
        print "Please enter a positive integer"

Thank you very much it WORKED!:D...but do you know how to do the second thing?

(If the user inserts a wrong answer he can try again.)

Code update.

import random
wish = "j"

while True:
      user = raw_input("How many multiplications ? ")
      try:
          max_multiplications = int(user)
          if max_multiplications <= 0:
              raise ValueError
          else:
              break  
      except ValueError:
  
          print "Please enter a positive integer"

while wish == "j":
    multi = 0
    right_answer = 0
    while max_multiplications != multi:

        randomint1 = random.randint(1, 100)
        randomint2 = random.randint(1, 100)

        print randomint1, "*", randomint2, "="
        ik = float(raw_input ("Insert your answer "))                  
        multi += 1                   
        answer = randomint1 * randomint2
        
        if ik == answer:
            right_answer += 1
            print "Correct!Good Job!"                                  
        else:
            print "Wrong answer!"
                             
   
    else:
        wish = raw_input("You want to try again? [j/e]")

Well, you can make a loop

trials = 3
        while trials > 0:
            trials -= 1
            print randomint1, "*", randomint2, "="
            ik = float(raw_input ("Insert your answer "))                  
            multi += 1                   
            answer = randomint1 * randomint2
        
            if ik == answer:
                right_answer += 1
                print "Correct!Good Job!"
                break
            elif trials > 0:
                print "Wrong answer! Try again!"
            else:
                print "Wrong answer!"
                break

Great it works!. But for example:
How manu multiplications do u want? ( I insert 2)
Then after the solving it asks if I want to try again and if I say "j", the program doesn't start from the beginning(how many m...do u want?) but it starts asking answers again...(2 multiplications again)

Do I have to change something here?:S

else:
        wish = raw_input("You want to try again? [j/e]"

Ok it's NOT working as it should, i don't know..I thought I got it working...but maybe you can spot the mistake :( Something is wrong where the trials are...that section..

import random
wish = "j"

while True:
      user = raw_input("How many multiplications ? ")
      try:
          max_multiplications = int(user)
          if max_multiplications <= 0:
              raise ValueError
          else:
              break  
      except ValueError:
  
          print "Please enter a positive integer"

while wish == "j":
    multi = 0
    right_answer = 0
    while max_multiplications != multi:

        randomint1 = random.randint(1, 10)
        randomint2 = random.randint(1, 10)

    trials = 3
    while trials > 0:
        trials -= 1
        print randomint1, "*", randomint2, "="
        ik = float(raw_input ("Insert your answer "))                  
        multi += 1                   
        answer = randomint1 * randomint2
        
        if ik == answer:
            right_answer += 1
            print "Correct!Good Job!"
            break
        elif trials > 0:
            print "Wrong answer! Try again!"
        else:
            print "Wrong answer!"
            break
                             
   
else:
    wish = raw_input("You want to try again? [j/e]")

Ok..I have 2 problems now.
1) When I want to solve again, I press "j" and it doesn't start from the beginning.(How many multiplicatios u want to solve?)
2) When I insert like, I want to solve 2 multiplications
4*1 =4
Correct
5*2=11
Wrong
5*2=12
Wrong
5*2=10
Correct
and then the program gives me like 6*7 but I wanted to solve 2 multiplications.

Help.:/

The lines 24 to 40 should have one more indentation. Also the multi += 1 should go before the trial loop.

Yes that helped!..but what about my problem 1).

use your brain.

:X
Well yeah..tried,but nothing..
Anyway thank you for your help.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.