簡體   English   中英

如何找到中位數?

[英]How to find the median?

我正在嘗試創建一個可以獲取任意數量的輸入並找到中位數的函數。 我覺得我要解決所有這些錯誤,我無法獲得此功能。
我需要做什么?

  1. 我列出了清單
  2. 我要求用戶輸入
  3. 我用numpy排序
  4. 我印了中位數

碼:

import numpy

numbers = [1,2,3]                
1 = input("Please type your first number")            
2 = input("please type your second number")       
3 = input("please type your third number")       
median = numpy.median(numbers)            
print(median) 

我要完成的工作:

您想找到什么數字的中位數? 1,2,3,4,5,6,7
中位數是:4

您應該將數字存儲在列表中,並使用循環將數字添加到列表中。

import numpy

size = int(input("Enter number of numbers you would like to enter"))
numbers = []
for i in range(size):
    numbers.append(int(input("Please type in number %d" % i)))

median = numpy.median(numbers)  

print(median) 

您的方法失敗,因為您嘗試將數字用作變量名,這是無效的。

您不能將值賦給1、2或3。此外,您需要將字符串從input轉換為int 嘗試一些簡單的更改。

import numpy                
input1 = input("Please type your first number")            
input2 = input("Please type your second number")       
input3 = input("Please type your third number")
numbers = map(int, [input1, input2, input3])
median = numpy.median(numbers)            
print(median) 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM