对浮点数使用isdigit吗?

编程入门 行业动态 更新时间:2024-10-11 23:26:39
本文介绍了对浮点数使用isdigit吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 a = raw_input('How much is 1 share in that company? ') while not a.isdigit(): print("You need to write a number!\n") a = raw_input('How much is 1 share in that company? ')

这仅在用户输入integer时有效,但是即使他们输入float,我希望它也能正常工作,但在输入string时无效.

This only works if the user enters an integer, but I want it to work even if they enter a float, but not when they enter a string.

因此,用户应该能够输入9和9.2,但不能输入abc.

So the user should be able to enter both 9 and 9.2, but not abc.

我应该怎么做?

推荐答案

使用正则表达式.

import re p = repile('\d+(\.\d+)?') a = raw_input('How much is 1 share in that company? ') while p.match(a) == None: print "You need to write a number!\n" a = raw_input('How much is 1 share in that company? ')

更多推荐

对浮点数使用isdigit吗?

本文发布于:2023-10-18 04:20:07,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1503082.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:浮点数   isdigit

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!