0

我需要进行错误检查以确保用户输入有两位小数。

我怎样才能做到这一点?

我最终做了这样的事情:

 if len(input) > 3:
      if input[-3] == ".":
           #then the validation of the varriable
4

1 回答 1

1

您似乎只需要一个数字、十进制和两个数字:

import re
s = '3.45'
if re.match("^\d{1}\.\d{2}$", s):
    print(s)
else:
    print('No match')

\d* 不匹配任何数字或任意数量。\d+ 匹配一个数字或任意数量。\d{2} 匹配两个数字。^ 从开头开始, $ 在结尾结束。

在 Python 2x 中, raw_input 返回一个字符串,而在 Python 3x 中,输入返回。

于 2014-05-03T22:02:07.287 回答