i = 0
sum = 0
list_num = []
while i <10:
ran_num = random.randint(1,20)
if ran_num not in list_num:
list_num.append(ran_num)
i+=1 #當條件滿足的時候在將i+1,來控制循環次數
sum+=ran_num
print(list_num,len(list_num),'十個隨機數的和是:%d' %sum )
print('-----------取出列表中最大值和最小值-------------')
min_num = list_num[0] #當list是空的時候不能這樣寫,會產生list index out of range
max_num = list_num[0]
for j in list_num:
if min_num > j: #這個位置不能這樣寫 if max_num < list_num[i]: 報錯TypeError: '<' not supported between instances of 'list' and 'int'
min_num = j
if max_num < j:
max_num = j
print('最大值{}'.format(max_num),'最小值{}'.format(min_num))
