如何在Python 3.3.0上编程模式

时间:2017-04-27 10:51:34

标签: python math mode

我需要帮助尝试在python 3.3中编写模式 我已经尝试了大约2个小时,这让我烦恼。 因为我使用3.3,所以我们没有统计模块,这通常是我如何解决这个问题而且我无法在学校计算机上更新。我的程序被设计为计算平均值,中位数,模式并退出。除模式外,他们全部工作。 有人有主意吗?它会有所帮助! 我所拥有的一切都是

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":

2 个答案:

答案 0 :(得分:1)

您可以在没有任何其他库的情况下执行以下操作:

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

答案 1 :(得分: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)]