编写一个Python 3应用程序,该应用程序包含一个名为Sort_and_Mean的用户定义函数。 Sort_and_Mean函数应:
已完成: -排序并返回传递给它的数字列表 -计算并返回列表的平均值。
我尝试去掉括号,但这没用。
import sys
def sort_and_mean(number):
for index in range(1,len(number)):
value = number[index]
i = index - 1
while i>=0:
if value < number[i]:
number[i+1] = number[i]
number [i] = value
i = i - 1
else:
break
return number
number = " "
while True:
print("\nSorting Numbers in Python 3 Program")
if (number == "exit" or number == "Exit" or number == "EXIT"):
print("Goodbye")
sys.exit()
else:
number = input("Enter a the value or type Exit to exit: ")
number = [int(v) for v in number.split(',')]
print("Original List: ", number)
print("Sorted List: ", sort_and_mean(number))
print ("Mean Value:", "%0.2f" % (sum(number) / len(number)))
预期: -原始清单:3,9,7,4,0,2 -排序列表:0,2,3,4,7,9 -平均值:4.17
退出结果:
Traceback (most recent call last):
File "/Users/antoniocazarez/Desktop/sortingnumbers.py", line 25, in <module>
number = [int(v) for v in number.split(',')]
File "/Users/antoniocazarez/Desktop/sortingnumbers.py", line 25, in <listcomp>
number = [int(v) for v in number.split(',')]
ValueError: invalid literal for int() with base 10: 'exit'
结果: 原始清单:[3、9、7、4、0、2] 排序列表:[0、2、3、4、7、9] 平均值:4.17