簡體   English   中英

如何停止並重復功能?

[英]How do I stop and repeat a function?

如果值> 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:而不是(1,10), if 0 < value < 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: ")

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM