Python how to only accept numbers as a input

后端 未结 5 522
野的像风
野的像风 2020-12-18 16:34
mark= eval(raw_input(\"What is your mark?\"))
try:
    int(mark)
except ValueError:
    try:
        float(mark)
    except ValueError:
        print \"This is not a         


        
5条回答
  •  再見小時候
    2020-12-18 17:33

    remove eval and your code is correct:

    mark = raw_input("What is your mark?")
    try:
        int(mark)
    except ValueError:
        try:
            float(mark)
        except ValueError:
            print "This is not a number"
    

    Just checking for a float will work fine:

    try:
        float(mark)
    except ValueError:
        print "This is not a number"
    

提交回复
热议问题