python中反复生成一系列随机数(范围在0.1到0.2之间),直到和大于10
使用random模块生产随机数并输出,每个随机数累加,和大于10时结束处理:
import random
total = 0
count = 0
while total < 10:
number = random.uniform(0.1, 0.2)
total += number
count += 1
print('%02d: %f' %(count,number))
print(f'生成了 {count} 个随机数,总和为 {total}')
import random
s = 0
while s <= 10:
s += random.uniform(0.1, 0.2)
print(s)
不知道你这个问题是否已经解决, 如果还没有解决的话:接下来的示例中我们新建了一个包含多个元素的列表,然后将只出现一次的元素 5 移除。
# Remove item that is present only once in the List
mylist = [21, 5, 8, 52, 21, 87]
item = 5
# remove the item
mylist.remove(item)
print(mylist)
执行和输出:
可以看出该项已移除,它后边的每项的索引减一。