Python中的菜单列表
我对Python非常陌生,所以我提前道歉!我正在尝试创建一个列表,其中用户可以选择添加数字、显示平均值、显示中值、按顺序打印列表、按相反顺序打印列表或退出。我以为我在正确的轨道上,但我不能让它运行。有人能帮我吗Python中的菜单列表,python,list,mean,median,Python,List,Mean,Median,我对Python非常陌生,所以我提前道歉!我正在尝试创建一个列表,其中用户可以选择添加数字、显示平均值、显示中值、按顺序打印列表、按相反顺序打印列表或退出。我以为我在正确的轨道上,但我不能让它运行。有人能帮我吗 def main(): myList = [ ] addOne(myList) choice = displayMenu() while choice != '6': if choice == '1': addOne
def main():
myList = [ ]
addOne(myList)
choice = displayMenu()
while choice != '6':
if choice == '1':
addOne(myList)
elif choice == '2':
mean(myList)
elif choice == '3':
median(myList)
elif choice == '4':
print(myList)
elif choice == '5':
print(myList)
choice = displayMenu()
print ("\nThanks for playing!\n\n")
def displayMenu():
myChoice = '0'
while myChoice != '1' and myChoice != '2' \
and myChoice != '3' \
and myChoice != '4' and myChoice != '5':
print("""\n\nPlease choose
1. Add a number to the list/array
2. Display the mean
3. Display the median
4. Print the list/array to the screen
5. Print the list/array in reverse order
6. Quit
""")
myChoice = input("Enter option---> ")
if myChoice != '1' and myChoice != '2' and \
myChoice != '3' and myChoice != '4' and myChoice != '5':
print("Invalid option. Please select again.")
return myChoice
#This should make sure that the user puts in a correct input
def getNum():
num = -1
while num < 0:
num = int(input("\n\nEnter a non-negative integer: "))
if num < 0:
print("Invalid value. Please re-enter.")
return num
#This is to take care of number one on the list: Add number
def addOne(theList):
while True:
try:
num = (int(input("Give me a number:")))
num = int(num)
if num < 0:
raise exception
print("Thank you!")
break
except:
print("Invalid. Try again...")
theList.append(num)
#This should take care of the second on the list: Mean
def mean(theList):
myList = []
listSum = sum(myList)
listLength = len(myList)
listMean = listSum / listLength
print("The mean is", listMean)
#This will take care of number three on the list: Median
def median(theList):
myList = []
return myList.median(theList.array(myList))
print("The median is", listMedian)
#This will take care of the fourth thing on the list: Print the list
def sort(theList):
theList.sort()
print(theList)
#This will take care of the fifth thing on the list
def reversesort(theList):
theList.sort(reverse=True)
print(theList)
main()
当我尝试选择选项2时,它会给我:
Traceback (most recent call last):
File "/Users/Gunter/Documents/CS 110/List and Traversal and Exception Handling.py", line 94, in <module>
main()
File "/Users/Gunter/Documents/CS 110/List and Traversal and Exception Handling.py", line 12, in main
mean(myList)
File "/Users/Gunter/Documents/CS 110/List and Traversal and Exception Handling.py", line 73, in mean
listMean = listSum / listLength
ZeroDivisionError: division by zero
Traceback (most recent call last):
File "/Users/Gunter/Documents/CS 110/List and Traversal and Exception Handling.py", line 94, in <module>
main()
File "/Users/Gunter/Documents/CS 110/List and Traversal and Exception Handling.py", line 14, in main
median(myList)
File "/Users/Gunter/Documents/CS 110/List and Traversal and Exception Handling.py", line 79, in median
return myList.median(theList.array(myList))
AttributeError: 'list' object has no attribute 'median'
当我尝试运行第三个选项时,它会给我:
Traceback (most recent call last):
File "/Users/Gunter/Documents/CS 110/List and Traversal and Exception Handling.py", line 94, in <module>
main()
File "/Users/Gunter/Documents/CS 110/List and Traversal and Exception Handling.py", line 12, in main
mean(myList)
File "/Users/Gunter/Documents/CS 110/List and Traversal and Exception Handling.py", line 73, in mean
listMean = listSum / listLength
ZeroDivisionError: division by zero
Traceback (most recent call last):
File "/Users/Gunter/Documents/CS 110/List and Traversal and Exception Handling.py", line 94, in <module>
main()
File "/Users/Gunter/Documents/CS 110/List and Traversal and Exception Handling.py", line 14, in main
median(myList)
File "/Users/Gunter/Documents/CS 110/List and Traversal and Exception Handling.py", line 79, in median
return myList.median(theList.array(myList))
AttributeError: 'list' object has no attribute 'median'
均值问题
您从回溯消息中得到了一个非常好的提示:ZeroDivisionError:DivisionbyZero。你们部门的分母是什么?列表长度。listLength是从哪里计算的?使用函数的本地作用域创建的空列表myList的长度。您需要将列表的长度作为参数传递给函数
你的平均数计算还有另一个问题:你的分子对除法也不正确。修复它是留给您的一项练习
中位数问题
同样,回溯消息给了您一个非常好的提示:AttributeError:'list'对象没有属性'median'。和均未提供中值方法。您需要将其定义为函数,或者构建一个类,将其定义为类方法,并将列表作为内部存储
或者,您可以避免重新发明轮子——这可能是所有答案中最具python风格的一个——并使用标准库中的模块。您需要更具体一些,而不是我无法让它运行。我已经尝试过复制/粘贴它,但有一些东西尚未定义。你有什么问题?这应该是一个独立的例子吗?如果我试图选择计算平均值或计算中位数,它就会爆炸。如果我尝试选择按顺序或反向打印列表,它只会显示一个空集。如果代码崩溃,它通常会在回溯中提供一个方便的错误线索。你能把它作为一个编辑包含在你的问题中吗?甚至没有运行代码,我的编辑器将listMedian显示为未定义的中间值,因此我知道它将无法运行。我只是在尝试运行平均值和中间值时发布了它所说的内容。谢谢你帮助我!