|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
本帖最后由 VanessaXD 于 2020-9-29 22:41 编辑
请教一下各位大神,我这串代码有什么问题应该改正?为什么在将输入的浮点数中的奇数替换成8的时候,一部分可以替换,一部分不行?比如说100.5可以被替换成800.8,而100.3,或者100.7,只能被替换成800.3和800.7?
- numlist=[]
- list1=[]
- new=''
- while True:
- a=input('')
- if a=='x':
- break
- else:
- numlist.insert(0,a)
- for i in range(len(numlist)):
- for j in range(len(numlist[i])):
- while True:
- if numlist[i][j]=='1' or numlist[i][j]=='3' or numlist[i][j]=='5' or numlist[i][j]=='7' or numlist[i][j]=='9':
- new=str(numlist[i])[:j]+'8'+str(numlist[i])[j+1:]
- print(new)
- del numlist[i]
- numlist.insert(0,new)
- continue
- else:
- break
- print(numlist)
- n=len(numlist)
- for z in range (n):
- c=0
- for l in range (0,n-1-z):
- if numlist[l] > numlist[l+1]:
- numlist[l],numlist[l+1] = numlist[l+1],numlist[l]
- c+=1
- if 0==c:
- break
- for u in numlist:
- print(u)
复制代码
不用replace就换一种,不用sort就用冒泡排序,和你写的也一样,但是你说不用append,那怎么添加进列表,那能不能用split呢?
看你这题,其实我7楼回复的就符合题意,要复杂点下面这样也可以
- list1 = []
- while True:
- a = input('请输入浮点:')
- if a == 'x':
- print('退出。')
- break
- else:
- new = ''
- for temp in list(a):
- if (temp != '.') and (int(temp) % 2 != 0):
- temp = '8'
- new = new + temp
- list1.append(float(new))
- for i in range(len(list1)):
- for j in range(0, len(list1) - i - 1):
- if list1[j] > list1[j + 1]:
- list1[j], list1[j + 1] = list1[j + 1], list1[j]
- for i in list1:
- print(i)
复制代码
|
|