如果用户输入单词时输出“无效输入”,该如何在代码中使用checkint?

时间:2019-04-25 17:15:26

标签: python

如果您输入单词而不是字母,我不知道如何将功能放置在输出“无效输入”的位置。我想使用检查,但是当我搜索它时,我没有希望。

counter = 0
import random
easy = input("Enter 1 for easy 2 for medium and 3 for hard")
if easy == "1":
    for i in range(10):
        x = random.randint(1,10)
        y = random.randint(1,10)
        ops = random.randint(1,3)    
        if ops == 1:
            print("What is the answer of ",x,"+",y,)
            answer = x+y
            ans = int(input())
            if ans == answer:
                print("Correct")
                counter = counter +1
            else:
                print("Incorrect")
        elif ops == 2:
            print("What is the answer of ",x,"-",y,)
            answer = x-y
            ans = int(input())
            if ans == answer:
                print("Correct")
                counter = counter +1
            else:
                print("Incorrect")
        elif ops == 3:
            print("What is the answer of ",x,"*",y,)
            answer = x*y
            ans = int(input())
            if ans == answer:
                 print("Correct")
                 counter = counter +1
            else:
                 print("Incorrect")        
    print("You got ",counter,"/10")

0 个答案:

没有答案