Python 数学测验随机数和大小,数值错误除外 来自随机导入randint user\u name=input(“\nHello,请输入您的姓名:”).title().strip() 打印(“好的”,用户名,“欢迎参加数学测验”) 分数=0 不正确=0 对于范围(1,11)内的问题数量: 尽管如此: 尝试: 大小=[“大”、“小”] rand=randint(0100) rand2=randint(0100) 大小\问题=randint(0,len(大小)-1) 打印('\n问题编号:{}'。格式(问题编号)) 问题_num+=1 打印(“哪个数字是“、大小[大小问题]、兰德”或“、兰德2”) 答案=int(输入(“你的答案是什么:”) 如果大小[大小问题]=“更大”: 如果rand>rand2且rand==回答: 打印(“正确”,兰德,“是”,尺寸[尺寸问题]) 分数+=1 elif rand2>rand和rand2==答案: 打印(“正确”,rand2,“是”,尺寸[尺寸问题]) 分数+=1 elif大小[大小问题]=“更小”: 如果randrand和rand2==答案: 打印(“正确”,rand2,“是”,尺寸[尺寸问题]) 分数+=1 伊里夫·兰德2!=答复: 打印(“不正确”) 如果得分>0: 分数-=1 埃利夫兰德!=答复: 打印(“不正确”) 如果得分>0: 分数-=1 elif大小[大小问题]=“更小”: 如果rand0: 分数-=1 除值错误外: 打印(“请仅输入数字”)

Python 数学测验随机数和大小,数值错误除外 来自随机导入randint user\u name=input(“\nHello,请输入您的姓名:”).title().strip() 打印(“好的”,用户名,“欢迎参加数学测验”) 分数=0 不正确=0 对于范围(1,11)内的问题数量: 尽管如此: 尝试: 大小=[“大”、“小”] rand=randint(0100) rand2=randint(0100) 大小\问题=randint(0,len(大小)-1) 打印('\n问题编号:{}'。格式(问题编号)) 问题_num+=1 打印(“哪个数字是“、大小[大小问题]、兰德”或“、兰德2”) 答案=int(输入(“你的答案是什么:”) 如果大小[大小问题]=“更大”: 如果rand>rand2且rand==回答: 打印(“正确”,兰德,“是”,尺寸[尺寸问题]) 分数+=1 elif rand2>rand和rand2==答案: 打印(“正确”,rand2,“是”,尺寸[尺寸问题]) 分数+=1 elif大小[大小问题]=“更小”: 如果randrand和rand2==答案: 打印(“正确”,rand2,“是”,尺寸[尺寸问题]) 分数+=1 伊里夫·兰德2!=答复: 打印(“不正确”) 如果得分>0: 分数-=1 埃利夫兰德!=答复: 打印(“不正确”) 如果得分>0: 分数-=1 elif大小[大小问题]=“更小”: 如果rand0: 分数-=1 除值错误外: 打印(“请仅输入数字”),python,python-3.6,Python,Python 3.6,也许你应该这样尝试。当有人键入的值不是您要查找的值时,您没有捕获错误 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:

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

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")
来自随机导入randint
user\u name=input(“\nHello,请输入您的姓名:”).title().strip()
打印(“好的”,用户名,“欢迎参加数学测验”)
分数=0
不正确=0
对于范围(1,11)内的问题数量:
尽管如此:
尝试:
大小=[“大”、“小”]
rand=randint(01100)
rand2=randint(01100)
大小\问题=randint(0,len(大小)-1)
打印('\n问题编号:{}'。格式(问题编号))
问题_num+=1
打印(“哪个数字是“、大小[大小问题]、兰德”或“、兰德2”)
答案=int(输入(“你的答案是什么:”)
如果大小[大小问题]=“更大”:
如果答案不是[rand,rand2]:
提升值错误(“提供的值不正确”)
如果rand>rand2且rand==回答:
打印(“正确”,兰德,“是”,尺寸[尺寸问题])
分数+=1
elif rand2>rand和rand2==答案:
打印(“正确”,rand2,“是”,尺寸[尺寸问题])
分数+=1
elif大小[大小问题]=“更小”:
如果答案不是[rand,rand2]:
提升值错误(“提供的值不正确”)
如果rand0:
分数-=1
我添加了一个
raiseexception
来捕获错误条件。但是,您可以选择不同的方式来捕获您的消息。这实际上结束了这个计划。但是如果你想继续,你可以用打印出来


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

为什么值错误不起作用?什么是“不起作用”?你的预期产出是什么?你得到了什么?您认为在哪里引发了
ValueError
异常?如果答案不正确,则不打印不正确!程序如何知道它不正确,在任何情况下都不会引发
ValueError
。异常块仅在引发该异常时执行。看起来您只需要条件逻辑,而不是异常逻辑。
ValueError
仅在用户未输入整数时才会在代码中引发,如果答案错误则不会引发。如果答案是错的,什么也不会发生。
while True
循环也没有任何东西可以打破它,这意味着这个co
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")
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