【问题标题】:Maths quiz random numbers and size, except ValueERROR数学测验随机数和大小,除了 ValueERROR
【发布时间】:2017-04-12 04:46:21
【问题描述】:
from random import randint

user_name = input("\nHello, please enter your name: ").title().strip()
print ("Alright",user_name,"Welcome to your maths quiz")
score=0
incorrect = 0 

for question_num in range(1, 11):
    while True:
        try:    
            size = ["bigger", "smaller"]
            rand = randint(0,100)
            rand2 = randint(0,100)
            size_question = randint(0, len(size) - 1)
            print('\nQuestion number: {}'.format(question_num))
            question_num += 1
            print ("Which number is", size[size_question], rand, "or", rand2)
            answer = int(input ("What is your answer: "))
            if size[size_question] == "bigger":
                if rand > rand2 and rand == answer:
                    print("correct", rand, "is", size[size_question])
                    score += 1 
                elif rand2 > rand and rand2 == answer:
                    print("correct", rand2, "is", size[size_question])
                    score += 1
            elif size[size_question] == "smaller": 
                if rand < rand2 and rand == answer:
                    print("correct", rand, "is", size[size_question])
                    score += 1
                elif rand2 < rand and rand2 == answer:
                    print("correct", rand2, "is", size[size_question])
                    score += 1   

        except ValueError:
            print ("Incorrect")
            if score > 0:
                score -= 1

【问题讨论】:

  • 为什么值错误不起作用?
  • “不工作”是什么意思?你的预期输出是什么,你得到了什么?您认为在哪里引发了 ValueError 异常?
  • 当答案不正确时,不正确不打印!
  • 程序如何知道它是不正确的,在任何时候都不会引发ValueError。异常块仅在引发该异常时才执行。看起来你只需要条件逻辑,而不是异常逻辑。
  • ValueError 仅在用户未输入整数时才会在您的代码中引发,如果答案错误则不会引发。如果答案是错误的,什么都不会发生。 while True 循环也没有什么可以破坏它的,这意味着这段代码将永远运行。正如@AChampion 所说,您需要使用条件逻辑,这里甚至不需要except 子句,您可以使用isdigit() 测试整数

标签: python python-3.6


【解决方案1】:
from random import randint

user_name = input("\nHello, please enter your name: ").title().strip()
print ("Welcome",user_name,"to the maths quiz")
score=0
incorrect = 0 
question_num = 1

while question_num <= 3:
    try:    
        size = ["bigger", "smaller"]
        rand = randint(0,100)
        rand2 = randint(0,100)
        size_question = randint(0, len(size) - 1)
        print('\nQuestion number: {}'.format(question_num))
        question_num += 1
        print ("Which number is", size[size_question], rand, "or", rand2)
        answer = int(input ("What is your answer: "))
        if size[size_question] == "bigger":
            if rand > rand2 and rand == answer:
                print("correct", rand, "is", size[size_question])
                score += 1 
            elif rand2 > rand and rand2 == answer:
                print("correct", rand2, "is", size[size_question])
                score += 1
            elif rand2 != answer:
                print ("Incorrect")
                if score > 0:
                    score -= 1  
            elif rand != answer:
                print ("Incorrect")
                if score > 0:
                    score -= 1             
        elif size[size_question] == "smaller": 
            if rand < rand2 and rand == answer:
                print("correct", rand, "is", size[size_question])
                score += 1
            elif rand2 < rand and rand2 == answer:
                print("correct", rand2, "is", size[size_question])
                score += 1                       
            elif rand2 != answer:
                print ("Incorrect")
                if score > 0:
                    score -= 1                
            elif rand != answer:
                print ("Incorrect")
                if score > 0:
                    score -= 1                    

    except ValueError:
        print ("Please enter numbers only")

【讨论】:

    【解决方案2】:

    也许你应该这样尝试。当有人输入的值不是您要查找的值时,您没有捕获到错误。

    from random import randint
    
    user_name = input("\nHello, please enter your name: ").title().strip()
    print ("Alright", user_name, "Welcome to your maths quiz")
    score = 0
    incorrect = 0
    
    for question_num in range(1, 11):
        while True:
            try:
                size = ["bigger", "smaller"]
                rand = randint(0, 100)
                rand2 = randint(0, 100)
                size_question = randint(0, len(size) - 1)
                print('\nQuestion number: {}'.format(question_num))
                question_num += 1
                print ("Which number is", size[size_question], rand, "or", rand2)
                answer = int(input("What is your answer: "))
                if size[size_question] == "bigger":
                    if answer not in [rand, rand2]:
                        raise ValueError("Incorrect value is supplied")
    
                    if rand > rand2 and rand == answer:
                        print("correct", rand, "is", size[size_question])
                        score += 1
                    elif rand2 > rand and rand2 == answer:
                        print("correct", rand2, "is", size[size_question])
                        score += 1
                elif size[size_question] == "smaller":
                    if answer not in [rand, rand2]:
                        raise ValueError("Incorrect value is supplied")
    
                    if rand < rand2 and rand == answer:
                        print("correct", rand, "is", size[size_question])
                        score += 1
                    elif rand2 < rand and rand2 == answer:
                        print("correct", rand2, "is", size[size_question])
                        score += 1
            except ValueError:
                print ("Incorrect")
                if score > 0:
                    score -= 1
    

    我添加了一个raise Exception 来捕获错误情况。但是,您可以选择不同的方式来捕获您的消息。这实际上结束了该计划。但如果你想继续,你可以使用打印出来。

    您还应该查看 python 的文档以了解何时应该使用try and except。希望这可以帮助。

    【讨论】:

      猜你喜欢
      • 2015-05-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-02-13
      • 2015-04-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多