2016-10-25 110 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感谢编辑!我错过了! –