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

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

最佳答案

您可以使用if 0 < value < 10:您可以通过调用 break 而不是 (1,10) 来停止循环.

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

关于python - 如何停止并重复某个功能?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18130289/

相关文章:

python-3.x - 有没有办法为 ipywidgets 中的容器设置标题值?

python - 在 python 中匹配来自 WhatsApp 日志的消息

python - 如何在 Python 中共享模块的数据(每分钟更新一次)?

c - 在包含字符串的 typedef 结构中使用指针

javascript - 在这段特定的 JavaScript 代码中返回一个函数有什么意义?

python - 我需要能够通过 USB 运行 pycharm + 模块

python - py2neo - 由于身份验证失败,客户端未经授权

python - 如何创建一个 UDF 来创建新列并修改现有列

javascript - 在javascript中需要帮助包装函数并正确处理 "this"

python-3.x - 如何创建 Python 安全 websocket 客户端请求?