如何检查用户输入是否为数字

时间:2017-10-12 01:12:21

标签: python python-3.x

我正在制作一个教育数学游戏,并希望确保所有用户输入都有效,即数字。 当他们输入字母或符号时,需要显示一条消息,说“无效,请再试一次”。有谁能够帮我? 这是我的代码:

import random

counter=0
score = 0
incorrect = 0

name=input("What is your name?")
print("Hi",name,",welcome to your math quiz!")

questions = ["10*2","4-2","6+12","6*4","12-5","6+54","1*0","3-6","4+0","65-9"]
answers=["20","2","18","24",'7','60','0','-3','4','56']

idx_questions = list(enumerate(questions))
idx_answers = list(enumerate(answers))

random.shuffle(idx_questions)

counter=0
inputs = []
for idxq, question in idx_questions:
    print("Question",counter+1,":",question)
    ans = input("What is the answer? ")
    counter=counter+1

    inputs.append(ans)
    for idxa, answer in idx_answers:
        if idxq == idxa and ans == answer:
            print("Correct")
            score=score+1
            print("Correct Answers=",score)
            print("Incorrect Answers=",incorrect)

        elif idxq == idxa and ans != answer:
            print("Incorrect. The answer is", answer)
            incorrect=incorrect+1
            print("Correct Answers=",score)
            print("Incorrect Answers=",incorrect)

print("End of quiz")
print(name,"your score is",score,"out of 10")
print(score*10,"/100",score,",%")
counter=0
while counter<10:
    print("Question",counter+1,": Your answer =", inputs[counter])
    counter=counter+1

2 个答案:

答案 0 :(得分:1)

你要找的是字符串类的isdigit方法。

即。 https://docs.python.org/3/library/stdtypes.html#str.isdigit

如果用户未输入有效数字,您可以再次申请有效号码:

def get_answer():
    num_str = input("Please enter a number: ").lower()
    if (num_str.isdigit()):
        return num_str
    else:
        print("Invalid, please try again")
        return get_answer()

嗨Jane,在用户输入数字之前,该功能基本上会一直要求输入。您需要做的就是使用函数获取ans变量,并保证它将是一个数字。我把它们放在一起为你下面。我的代码实际上有一些错误(对不起!)我修复了。您的工作代码如下所示:

import random

counter=0
score = 0
incorrect = 0

name=input("What is your name?")
print("Hi",name,",welcome to your math quiz!")

questions = ["10*2","4-2","6+12","6*4","12-5","6+54","1*0","3-6","4+0","65-9"]
answers=["20","2","18","24",'7','60','0','-3','4','56']

idx_questions = list(enumerate(questions))
idx_answers = list(enumerate(answers))

random.shuffle(idx_questions)

def get_answer():
    num_str = input("Please enter a number: ").lower()
    if (num_str.isdigit()):
        return num_str
    else:
        print("Invalid, please try again")
        return get_answer()

counter=0
inputs = []
for idxq, question in idx_questions:
    print("Question",counter+1,":",question)
    ans = get_answer()
    counter=counter+1

    inputs.append(ans)
    for idxa, answer in idx_answers:
        if idxq == idxa and ans == answer:
            print("Correct")
            score=score+1
            print("Correct Answers=",score)
            print("Incorrect Answers=",incorrect)

        elif idxq == idxa and ans != answer:
            print("Incorrect. The answer is", answer)
            incorrect=incorrect+1
            print("Correct Answers=",score)
            print("Incorrect Answers=",incorrect)

print("End of quiz")
print(name,"your score is",score,"out of 10")
print(score*10,"/100",score,",%")
counter=0
while counter<10:
    print("Question",counter+1,": Your answer =", inputs[counter])
    counter=counter+1

如果您有任何疑问,请提出申请!

答案 1 :(得分:0)

isdigit()检查字符串是否由数字组成。 您可以在Python Docs中找到它。

示例:

string1 = '1234'
string.isdigit()

返回True。

string2 = 'abcd'
string.isdigit()

返回False。