2017-04-27 36 views
-3

我需要帮助,试图在python 3.3中编程模式我一直在尝试大约2个小时,它正在扰乱我。 因为我使用3.3,所以我们没有统计模块,这通常是我如何解决这个问题,我无法在学校计算机上更新它。我的程序被假设为计算平均值,中值,模式并且也退出。他们都工作除了模式。 任何人有任何想法?它会帮助! 所有我迄今是如何在Python上编程模式3.3.0

lists = [1, 2, 3, 4, 5] 
print("Hello! What Is Your Name?") 
name = input() 
func = ["Average", "Median", "Quit", "Mode"] 
print("Please Enter 5 Numbers") 
lists[0] = input() 
lists[1] = input() 
lists[2] = input() 
lists[3] = input() 
lists[4] = input() 
print("Hello " + name + ", Would You Like " + func[0] + ", " + func[1] + ", " + func[2] + " Or, Would You Like to " + func[3]) 
func1 = input() 
if func1 == "Average" : 
    total = int(lists[0]) + int(lists[1]) + int(lists[2]) + int(lists[3]) 
    total1 = total/4 
    print("Your Average is " + str(total1)) 
elif func1 == "Median" : 
    lists.sort() 
    print("Your Median Is " + lists[2] + "!") 
elif func1 == "Quit": 
    print("Thank You") 
elif func1 == "Mode": 
+3

参见[查找一个列表的模式](http://stackoverflow.com/questions/10797819/finding-the-mode-of-a-list) – roganjosh

+0

你的行中有一个意想不到的空间'print(“Y我们的平均值是“+ str(total1))'。平均功能不起作用,改为'total1 = total/4.' – Nuageux

+0

如果你已经尝试了两个小时,为什么不展示你的一些努力?上面的代码甚至不尝试计算模式。 –

回答

1

你可以做以下的无需任何额外的库:

lists = [1, 2, 2, 4, 4, 5, 7] 
print(max(set(lists), key=lists.count)) 
+0

然而,我刚刚尝试做到这一点,它已成为“无效的语法”,并突出显示最大值,任何方式呢? – Jack

+0

oops我的不好,python3中的'print'需要'()'。我编辑了答案。 – Jeril

+0

谢谢兄弟!这工作! – Jack

0

看一看collections.Counter()及其方法most_common()

>>> from collections import Counter 
>>> lists = [1, 2, 2, 4, 4, 5, 7] 
>>> mode = Counter(lists).most_common(1) 
>>> mode 
[(2, 2)] 
+0

非常感谢您的帮助!它的工作原理:) – Jack

相关问题