如何停止并重复某个功能?

时间:2013-08-08 15:39:06

标签: python function python-3.x range

如果值为>如何停止程序? 10或者< 0并显示错误然后再次显示问题?

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

1 个答案:

答案 0 :(得分:1)

您可以使用if 0 < value < 10:代替(1,10),可以通过调用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: ")