2016-10-25 78 views
-2

回覆總數然後將其打印在輸出中的模塊化python有問題。幫一幫?return python

def main(): 
    Monday = int(input("Enter the store sales for Monday: ")) 
    Tuesday = int(input("Enter the store sales for Tuesday: ")) 
    Wednesday = int(input("Enter the store sales for Wednesday: ")) 
    Thursday = int(input("Enter the store sales for Thursday: ")) 
    Friday = int(input("Enter the store sales or Friday: ")) 

    total = totalSales() 
    print("the total sales for the week are:", total) 


def totalSales(Monday, Tuesday, Wednesday, Thursday, Friday): 
    weeklyTotal = Monday + Tuesday + Wednesday + Thursday + Friday 
    return weeklyTotal 

main() 

錯誤消息:

Enter the store sales for Monday: 5 
Enter the store sales for Tuesday: 4 
Enter the store sales for Wednesday: 6 
Enter the store sales for Thursday: 2 
Enter the store sales or Friday: 8 
Traceback (most recent call last): 
    File "so.py", line 16, in <module> 
    main() 
    File "so.py", line 8, in main 
    total = totalSales() 
TypeError: totalSales() takes exactly 5 arguments (0 given) 
+0

也許,如果你說你的「麻煩」的性質,有人可以幫助你。 –

+1

你還沒有指定你的問題是什麼。但我敢打賭,你面臨的主要問題是你沒有將所需的參數傳遞給'totalSales()'。看看你如何定義函數,並看看你是如何調用它。 – idjaw

+0

即時嘗試將總銷售額中的所有銷售總額加起來,然後將其返回 – vtecjustkickedinyo

回答

0

你有所需的信息傳遞給你的函數。你有五個輸入參數。當你調用函數時,你必須給它五個值。

total = totalSales(Monday, Tuesday, Wednesday, Thursday, Friday) 

...在你主要應該解決這個問題。


DETAIL

我看到,當我運行你的代碼看起來像這樣的問題。 注意錯誤信息的最後一行:它描述了問題。 將來,請記住在您的發佈中包含此信息。

Enter the store sales for Monday: 5 
Enter the store sales for Tuesday: 4 
Enter the store sales for Wednesday: 6 
Enter the store sales for Thursday: 2 
Enter the store sales or Friday: 8 
Traceback (most recent call last): 
    File "so.py", line 16, in <module> 
    main() 
    File "so.py", line 8, in main 
    total = totalSales() 
TypeError: totalSales() takes exactly 5 arguments (0 given) 
0

您需要將參數傳遞給totalSales函數。 當您使用參數調用函數時,請記得將它們包含在調用中。作爲一個非常簡單的例子:

功能

def AddNumbers(x, y): 
    return x + y 

呼叫

AddNumbers(1,2) #<-1 and 2 represent x and y in the above function 

在你的代碼已經聲明爲函數:

def totalSales(Monday, Tuesday, Wednesday, Thursday, Friday): 

但當你稱它你不知道傳遞任何的參數:

total = totalSales() 

它應該是:

def main(): 
    Monday = int(input("Enter the store sales for Monday: ")) 
    Tuesday = int(input("Enter the store sales for Tuesday: ")) 
    Wednesday = int(input("Enter the store sales for Wednesday: ")) 
    Thursday = int(input("Enter the store sales for Thursday: ")) 
    Friday = int(input("Enter the store sales or Friday: ")) 

    total = totalSales(Monday, Tuesday, Wednesday, Thursday, Friday) 
    print("the total sales for the week are:", total) 


def totalSales(Monday, Tuesday, Wednesday, Thursday, Friday): 
    weeklyTotal = Monday + Tuesday + Wednesday + Thursday + Friday 
    return weeklyTotal 

main() 
0

你是不是傳遞必需的參數,以您的totalSales()功能,因爲它需要5個參數,你不給任何。由於輸出看起來很奇怪,我還修復了您的打印聲明。

代碼:

def main(): 
    Monday = int(input("Enter the store sales for Monday: ")) 
    Tuesday = int(input("Enter the store sales for Tuesday: ")) 
    Wednesday = int(input("Enter the store sales for Wednesday: ")) 
    Thursday = int(input("Enter the store sales for Thursday: ")) 
    Friday = int(input("Enter the store sales or Friday: ")) 

    total = totalSales(Monday, Tuesday, Wednesday, Thursday, Friday) # Added arguments to function 
    print("the total sales for the week are: %s" % total) # Fixed Print 


def totalSales(Monday, Tuesday, Wednesday, Thursday, Friday): 
    weeklyTotal = Monday + Tuesday + Wednesday + Thursday + Friday 
    return weeklyTotal 

main() 
+0

@ cricket_007感謝編輯!我錯過了! –