python判断整数浮点数_Python:分析输入,看它是整数、浮点数还是字符串

为了判断输入是整数、浮点数还是字符串,我已经研究了一两天了。

简言之,程序的设计是将每个输入变成一个字符串,循环遍历每个字符串并检查列表数字。如果字符串的所有数字都是整数,如果它有一个“.”则是浮点数,如果没有,则不是数字。明显的缺陷是包含字母和“.”的字符串,在该程序中将被视为浮点数。

这个程序的最终目标是打开文本文件并查看什么是int、float或其他输入。

问题

-有没有办法进一步优化这个程序

-如何进一步修改此程序以打开文本文件,读取、分析和写入哪个输入在哪个列表中

第一篇帖子!!!#Checks input to see if input is integer, float, or character

integer = []

float = []

not_number = []

digits = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']

input_list = [100, 234, 'random', 5.23, 55.55, 'random2']

for i in input_list:

i = str(i)

length = len(i)

count = 0

marker = 0

for j in i:

for k in digits:

if k == j:

count = count + 1

#k loops through digits to see if j single character

#string input is number

if count == length:

integer.append(i)

marker = 1

#count is equal to length if entire string is integers

if j == '.':

float.append(i)

marker = 1

#Once '.' is found, input is "considered" a float

if marker == 1:

break

else:

not_number.append(i)

#If code above else proves that input is not a number the

#only result is that it isn't a number

print ('Integers: ', integer)

print ('Float: ', float)

print ('Not Numbers', not_number)

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值