剛剛開始使用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例如,它不能正確顯示,
可以顯示具有'getProd'函數和'isum3'定義的代碼的版本嗎? – Kevin
我剛剛添加了用於計算用戶輸入的3個數字乘積的getProd行。 – JuanB457
我認爲在編輯過程中可能會丟失一些東西。我看到標題已經改變,但代碼仍然相同。 – Kevin