Discuss / Python / 小白求问

小白求问

Topic source

L = list(input('请输入list:'))

def findMinAndMax(L): if len(L) == 0: return (None,None) elif len(L) ==1: return (L[0],L[0]) else: min = L[0] max = L[0] for a in L: if a <= min: min = a elif a >= max: max = a return (max,min) print('L的最大值和最小值为:{}'.format(findMinAndMax(L)))

输出结果一直是:('9', ','),为什么呢?如果我把input改为直接给L 一个list,又可以直接输出正确结果。

input() 返回了一个字符串,list(s) 会把字符串转换成单个字符组成的列表,比如 list('1,2') 返回 ['1', ',', '2']

正确的做法是

L = list(map(int, input('请输入list:').split(',')))

Storm45788

#3 Created at ... [Delete] [Delete and Lock User]

加空格输入:L=input().split(' ') 按空格分割 不加空格输入:L=list(input())


  • 1

Reply