2013-11-25 43 views
0

剛剛開始使用python 2.7.5中的函數,爲學校工作但是輸出正確結果時出現問題,我想要用戶輸入3個號碼並獲得他們的產品。我可以得到2個數字的總和,但我的問題是我可以從用戶那裏得到輸入,但輸出要麼顯示不正確,要麼出現錯誤,以下是我的代碼:(我很新編碼所以請儘可能詳細)python 2.7.5,使用函數來處理用戶輸入,分配新變量和多個函數

#first function for getting numbers 
def getValue(): 
    num = float(raw_input("Enter a number: "))  
    return num  

#The second function to calculate the total 
def getSum(first, second): 
    total = first + second 
    return total 
def getProd(n1,n2,n3): #my attempt for getting the product of 3 numbers entered 
    itotal = n1 * n2 * n3 

#The third function to display the data 
def displayData(first, second, total): 
    print("The first value is = ", first); 
    print("The second value is = ", second); 
    print("The Total is = ", total); 

def main(): 
    isum1 = 0 # need to initialize the needed variables 
    isum2 = 0 #how do I enter for a third number for the product? 
    isum3 = 0 
    totalSum = 0 

    isum1 = getValue() #call the function to get a number 
    isum2 = getValue() 
    isum3 = getValue() 
    itotal = getProd(isum1, isum2, isum3) #declared itotal as var for getting the 
              #getProd function 

    totalSum = getSum(isum1, isum2) 

    displayData(isum1, isum2, totalSum) #unclear how to write the display correctly 
main() 

我的問題是我需要的產品功能,但似乎無法得到它正確的。如果我把一個isum3和一個新的函數定義爲getProd例如,它不能正確顯示,

+2

可以顯示具有'getProd'函數和'isum3'定義的代碼的版本嗎? – Kevin

+0

我剛剛添加了用於計算用戶輸入的3個數字乘積的getProd行。 – JuanB457

+0

我認爲在編輯過程中可能會丟失一些東西。我看到標題已經改變,但代碼仍然相同。 – Kevin

回答

1

你的getProd是一個好的開始,但你必須要return東西是有用的。

def getProd(n1,n2,n3): 
    itotal = n1 * n2 * n3 
    return itotal 

displayData不適合用於顯示產品的數據,因爲它提到了一個「總」和僅具有兩個變量。你可以寫有類似行爲的另一種方法,但是:

def displayProductData(first, second, third, total): 
    print("The first value is = ", first); 
    print("The second value is = ", second); 
    print("The third value is = ", third) 
    print("The Total is = ", total); 

最後,你需要更新你的main,使其正確使用新方法。

def main(): 
    isum1 = 0 
    isum2 = 0 
    isum3 = 0 

    isum1 = getValue() 
    isum2 = getValue() 
    isum3 = getValue() 
    itotal = getProd(isum1, isum2, isum3) 

    displayProductData(isum1, isum2, isum3, itotal) 

其他雜項改進

Python style guide建議命名變量和函數的時候,而不是使用mixedCaselower_case_with_underscores

def get_value(): 
    #code 

def get_sum(): 
    #code 

def get_prod() 

在功能,它可以直接返回一個表達式,而不必首先將它分配給一個變量。

def get_sum(first, second): 
    return first + second 

def get_prod(n1,n2,n3): 
    return n1 * n2 * n3 

main,你寫的你「需要初始化變量需要」,但事實上,你做必須這樣做,在一個函數的開始!你可以隨意分配一個變量。只是不要嘗試使用尚未定義的變量。

def main(): 
    isum1 = getValue() 
    isum2 = getValue() 
    isum3 = getValue() 
    itotal = getProd(isum1, isum2, isum3) 

    displayProductData(isum1, isum2, isum3, itotal) 

如果您正在使用Python 2和Python的不是3,然後print不需要括號。事實上,輸出結果可能會更好,因爲沒有括號,它不會將您的表達式解釋爲元組。

def display_product_data(first, second, third, total): 
    print "The first value is = ", first 
    print "The second value is = ", second 
    print "The third value is = ", third 
    print "The Total is = ", total 

曾經是輸出:

('The first value is = ', 2.0) 
('The second value is = ', 3.0) 
('The third value is = ', 4.0) 
('The Total is = ', 24.0) 

現在是:

The first value is = 2.0 
The second value is = 3.0 
The third value is = 4.0 
The Total is = 24.0  

你不需要使用分號。他們只是爲了讓用戶從其他語言中獲得舒適。並且在一行上劃分多條語句,但這不是很常見。