数学测验随机数和大小,ValueERROR除外

时间:2017-04-12 04:46:22

标签: python python-3.6

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

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

可能你应该这样试试。当有人输入您正在寻找的值以外的值时,您不会捕获错误。

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。希望这可以帮助。