2015-10-22 33 views
1

我一直在嘗試編寫平均值,中值和模式計算器。我有兩個問題。 1)我如何將用戶輸入的數字添加到空列表中。如果我只是衝入數字,它給了我這個錯誤:如何將用戶的數字添加到列表中

userNumbers = int(raw_input("Enter the digits here: ")) 
ValueError: invalid literal for int() with base 10: '2, 4, 6, 8' 

我很想知道如何避免這種情況。其次,我正在使用Python 2.7.10,並將很快轉移到3.4。我知道在3.4中有一個名爲statistics的模塊,它具有中值函數,但它在2.7.10上不起作用。出於好奇,我如何找到沒有這個功能的中位數。感謝大家!

from collections import Counter 


def numberPlacement(): 
    print("Welcome to the PyCalculator, please enter a series of digits!") 
    userNumbers = int(raw_input("Enter the digits here: ")) 
    userNumbers = [] 


def userChoice(): 
    while True: 
     print("Awesome, now that we haave your numbers please choose an operation (Mean, Median, or Mode!)") 
     userAnswer = raw_input("Enter your choice here: ") 
     if userAnswer == "Mean": 
      mean = sum(userNumbers)/float(len(userNumbers)) 
      return mean 
     elif userAnswer == "Mode": 
      mode = Counter(userNumbers) 
      return mode 
     continue 

print numberPlacement() 
print userChoice() 

回答

1

您收到的錯誤是因爲您試圖將無效字符強制轉換爲int。

您的輸入是:1, 2, 3, 4所以這些逗號是導致該錯誤的原因。

要糾正這個問題,只需刪除你的int()類型,通過在你的輸入中附加一個.split(',')來使你的輸入成爲一個列表。該分割所做的是通過分割逗號將字符串轉換爲列表。這樣你會得到一個由字符串表示的數字列表。

因此,由於您正在進行數學計算,因此您需要確保實際使用的是數值表示形式,而不是列表中數據的字符串表示形式。你可以通過使用Python的map方法()來完成這項工作,它可以將所有條目轉換爲int。

所以,你的用戶輸入,可以簡單地是這樣的:

的Python 3:

userNumbers = list(map(int, input("Enter the digits here: ").split(','))) 

的Python 2:

userNumbers = map(int, raw_input("Enter the digits here: ").split(',')) 

。在你的代碼中的另一個問題。在您的userChoice方法中,您引用了userNumbers。我認爲這是在numberPlacement方法中對userNumbers的引用。你不能這樣做。

我建議做的事情,因爲它好像你想要分別調用這兩個方法,是讓你的userChoice方法採取一個參數,這將是一個名爲userNumber的列表。所以,簡單地說:

def userChoice(userNumbers): 
    # the rest of your code 

現在,它是要記住很重要的,這userNumbersuserChoice方法是不一樣的你numberPlacement方法userNumbers。要理解爲什麼,這是範圍界定的一部分。你可以閱讀關於here

這樣,那麼,當你現在打電話給你的方法,你就必須做這樣的事情:

number_placements = numberPlacement() 
print(number_placements) 
print(userChoice(number_placements)) 

當你把它放在一起,你的代碼現在看起來像這樣:

(Python的2)

from collections import Counter 


def numberPlacement(): 
    print("Welcome to the PyCalculator, please enter a series of digits!") 
    userNumbers = map(int, raw_input("Enter the digits here: ").split(',')) 
    return userNumbers 


def userChoice(userNumbers): 
    while True: 
     print("Awesome, now that we haave your numbers please choose an operation (Mean, Median, or Mode!)") 
     userAnswer = raw_input("Enter your choice here: ") 
     if userAnswer == "Mean": 
      mean = sum(userNumbers)/float(len(userNumbers)) 
      return mean 
     elif userAnswer == "Mode": 
      mode = Counter(userNumbers) 
      return mode 
     continue 


r = numberPlacement() 
print(userChoice(r)) 

演示:

Welcome to the PyCalculator, please enter a series of digits! 
Enter the digits here: 1,2,3,4 
Awesome, now that we haave your numbers please choose an operation (Mean, Median, or Mode!) 
Enter your choice here: Mode 
Counter({1: 1, 2: 1, 3: 1, 4: 1}) 
+0

@Jack歡迎您的變量。讓我知道它是否適合你。 – idjaw

+0

不幸的是,我測試了代碼,它給了我同樣的錯誤。 – Jack

+0

@Jack它適合我。我把你的代碼放在我的答案中。看看這裏:http://pastebin.com/VGb9zhwD – idjaw

1

你需要分割你的輸入:

a = raw_input("Enter the digits here: ").split(',') 
b = [int(x) for x in a] 

然後,你可以參考你的號碼。

1

要打印不需要打印隨機的東西,而你也有局部範圍你想叫全球

def numberPlacement(): 
    print("Welcome to the PyCalculator, please enter a series of digits, comma seperated!") 
    userNumbers = raw_input("Enter the digits here: ") 
    return [ int(u.strip()) for u in userNumbers.split(',') ] 


def userChoice(userNumbers): 
    print("Awesome, now that we haave your numbers please choose an operation (Mean, Median, or Mode!)") 
    userAnswer = raw_input("Enter your choice here: ") 

    if userAnswer == "Mean": 

     mean = sum(userNumbers)/float(len(userNumbers)) 
     return mean 

    #omitted 

userNumbers = numberPlacement() 
print(userChoice(userNumbers)) 



#list comprehension rewritten as a basic for loop in response to question in comment 

final_list = [] 
for u in userNumbers.split(','): 
    x = u.strip() 
    y = int(x) 
    final_list.append(y) 

return final_list 
+0

感謝您將所有代碼放在一起! – Jack

+0

還有一個問題,在第四行,你能解釋你做了什麼嗎?我現在只編了一個星期左右的編碼,所以我還是新手!謝謝! – Jack

+0

這就是所謂的列表理解,對於所有的來龍去脈來說,谷歌是一個比我更好的老師。取決於你如何構建它們,它們可以用於很多不同的事情。在這個例子中,它基本上是一個for循環。嘗試閱讀它「將字符串usernumbers拆分爲逗號,並對結果列表中的每個元素,刪除前導/尾隨空格,將其轉換爲整數,返回所有最終結果的列表。」如果你願意,你可以把它寫成for循環。 – tlastowka

相關問題