1.将下面列表中各个元素的第二个字符取出来并且按顺序排列
word = [‘abc’,‘blibli’,‘DNA’,‘Aoe’,‘Cent’,‘ed’]
word = ['abc','blibli','DNA','Aoe','Cent','ed']
list = []
for i in word:
list.append(i[1])
list.sort()
print(list)
2.输入一个精度为0.01的浮点数,根据四舍五入保存它的值,使其精度为0.1
a = float(input("请输入一个精度为0.01的浮点数:"))
if a*100%10 >= 5:
a = int(a*10+1)/10
else:
a = int(a*10)/10
print(a)
3.任意输入一个整数,打印出从它本身一直到零的每个数
如果输入浮点数会报错,并提示重新输入
def number():
for i in range(1):
try:
a = int(input("请输入一个整数:"))
if a > 0:
while a != -1:
print(a)
a -= 1
elif a < 0:
while a != 1:
print(a)
a += 1
elif a == 0:
print(a)
except Exception as e:
print("你输入的不是一个整数,请重新输入:")
print("===========================")
number()
break
number()
4.根据第一题列表中元素中的第二个字符,有序排列列表
不用考虑第二个字符一样的情况
lis = ['abc','blibli','DNA','Aoe','Cent','ed']
for i in lis:
for j in range(5):
if lis[j][1] > lis[j+1][1]:
sum = lis[j]
lis[j] = lis[j+1]
lis[j+1] = sum
print(lis)