本帖最后由 ooxx7788 于 2017-3-30 19:00 编辑 import random
temp = [random.randint(1, 100) for i in range(10)]
print(temp)
temp[temp.index(max(temp))], temp[temp.index(min(temp))], temp[0], temp[-1] = temp[0], temp[-1],temp[temp.index(max(temp))], temp[temp.index(min(temp))]
print(temp)
其实只需要一句!
输出[69, 35, 51, 29, 58, 3, 16, 53, 90, 28]
[90, 35, 51, 29, 58, 28, 16, 53, 69, 3]
omg,我发现了自己的一个错误!留下来就当错误示范吧!
以下答案是我修正过的,带自我检查的,不知道还有没有错。以上代码,证明了一句,步子太大容易扯着蛋!import random
counts = 100
while counts:
temp = [random.randint(1, 100) for i in range(10)]
total = sum(temp)
print(temp)
temp[temp.index(max(temp))], temp[0] = temp[0], temp[temp.index(max(temp))]
temp[temp.index(min(temp))], temp[-1] = temp[-1], temp[temp.index(min(temp))]
print(temp)
if total != sum(temp):
print('There is something wrong!')
break
counts -= 1
|