【问题标题】:How do I stop and repeat a function?如何停止和重复一个功能?
【发布时间】:2013-08-08 15:39:05
【问题描述】:

如果值 > 10 或

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

【问题讨论】:

  • value 不会等于元组 (1, 10)。你的意思是1 &lt;= value &lt; 10

标签: python function python-3.x range


【解决方案1】:

你可以用if 0 &lt; value &lt; 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: ")

【讨论】:

  • 这只是break 退出for 循环的第一次迭代。如果您正在寻找这个,为什么要保持循环?
  • @SukritKalra 实际上break 是为了打破while 循环。
  • 第一个答案的缩进不同。
猜你喜欢
  • 1970-01-01
  • 2012-05-19
  • 2012-08-10
  • 1970-01-01
  • 1970-01-01
  • 2022-12-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多