Stevenson
Stevenson

Reputation: 637

How do I stop and repeat a function?

How do I stop the program if value is > 10 or < 0 and display Error and then display the question again?

import random

while True:

    value = int(input("Enter the amount of questions would you like to answer: "))   
    if value == (1,10):
        for i in range(value):
            numb1 = random.randint(0, 12)
            numb2 = random.randint(0, 12)
            answer = numb1 * numb2

            problem = input("What is " + str(numb1) + " * " + str(numb2) + "? ")

            if int(problem) == answer:
                print("You are Correct! Great Job!")
            elif int(problem) > answer:
                print("Incorrect, Your answer is too high!")
            elif int(problem) < answer:
                print("Incorrect, your answer is too low!")
    else:
        print(" Error, Please tpye a number 1-10 ")

Upvotes: 1

Views: 240

Answers (1)

suhailvs
suhailvs

Reputation: 21680

you can use if 0 < value < 10: instead of (1,10), you can stop the loop by calling break.

import random

while True:
    value = int(input("Enter the amount of questions would you like to answer: "))
    if 0 < value < 10:
        for i in range(value):
            numb1 = random.randint(0, 12)
            numb2 = random.randint(0, 12)
            answer = numb1 * numb2

            problem = input("What is {0} * {1}? ".format(numb1, numb2))

            if int(problem) == answer:
                print("You are Correct! Great Job!")
            elif int(problem) > answer:
                print("Incorrect, Your answer is too high!")
            elif int(problem) < answer:
                print("Incorrect, your answer is too low!")
        break

    print("Error, please type a number from 1 to 10: ")

Upvotes: 1

Related Questions