python程序设计

2025-01-04 20:35:50
推荐回答(2个)
回答1:

# 如果能利用正则表达式(re模块)可能程序会更简单些。str_test = input("please enter a string: ")buf_float = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '.'] def fun(test): buf_str = list(test) for m in range(0, len(buf_str)): if buf_str[m] not in buf_float: buf_str[m] = ' ' for n in range(0, len(buf_str)): if buf_str[n] == '.': if n == len(buf_str)-1: buf_str[n] = ' ' elif n != len(buf_str)-1 and buf_str[n+1] == ' ' or buf_str[n+1] == '.': buf_str[n] = ' ' return buf_str # ''.join():将列表合并为字符串# str.split():将字符串str按照空格拆分为字符串列表str_float = ''.join(fun(str_test)).split()if str_float == []: print('Not Found!')else: for i in str_float: print(i)

实验结果:
please enter a string: I have 5.67yuan. You have 5.68.
5.67
5.68

回答2:

不难,就是费点时间