def sum_num()
numbers=input("Please enter a sequence of comma-separated numbers:\n")
mynumbers=numbers.split(",")
print(mynumbers)
mylist=[]
maxnum= max(mynumbers)
minnum = min(mynumbers)
print(int(maxnum)+int(minnum))
return sum_num()
为什么define函数不起作用? 真的很困惑!
现在,正如您提到的,我将代码更改如下:
def sum_num():
numbers=input("Please enter a sequence of comma-separated numbers:\n")
mynumbers=numbers.split(",")
print(mynumbers)
mylist=[]
maxnum= max(mynumbers)
minnum = min(mynumbers)
print(int(maxnum)+int(minnum))
sum_num()
对此仍然感到困惑。谁能告诉我如何调用该函数进行测试?
答案 0 :(得分:1)
尝试一下:
def sum_num():
numbers = input("Please enter a sequence of comma-separated numbers:\n")
mynumbers = [int(x) for x in numbers.split(",") if x] # handling empty strings
min_num, max_num = min(mynumbers), max(mynumbers)
return min_num + max_num
print(sum_num())
答案 1 :(得分:1)
您必须在整数列表中找到min
和max
。因此,将您的字符串列表映射到整数列表。
我也将像下面那样重新排序代码,只是因为现在sum_num
可以在任何列表上调用:
def sum_num(lst):
return max(lst) + min(lst)
numbers = input("Please enter a sequence of comma-separated numbers:\n")
mynumbers = numbers.split(",")
print(sum_num(list(map(int, mynumbers))))
答案 2 :(得分:0)
如何?
def sum_num():
numbers=input("Please enter a sequence of comma-separated numbers:\n")
print(numbers)
maxnum= max(numbers)
minnum = min(numbers)
print(int(maxnum) + int(minnum))
sum_num()
答案 3 :(得分:0)
我认为您应该编辑
return sum_num()
到
return int(maxnum)+int(minnum)
获取这些值的总和。
和
print(sum_num())
您可以使用此代码来打印该值。
def sum_num():
numbers=input("Please enter a sequence of comma-separated numbers:\n")
mynumbers=numbers.split(",")
print(mynumbers)
mylist=[]
maxnum= max(mynumbers)
minnum = min(mynumbers)
print(int(maxnum)+int(minnum))
return int(maxnum)+int(minnum)
答案 4 :(得分:0)
除了使用内置的sum
,min
和max
,您还可以sort
列表并返回0
和{{1 }}
-1