我调用的def函数没有实现

时间:2016-07-17 14:27:28

标签: python function

所以,我基本上试图制作一个程序来计算五个数字的平均值。我调用了def函数,但它没有做任何事情。我在cmd上运行项目,只是它坐在那里并创建另一个输入行。巴姆,没有计算,没有任何东西。代码似乎没有问题,因为编译器没有指出一个。随意变得批评和粗鲁,这对我来说都很好。我是初学者,所以我可能犯了一些真正的愚蠢错误。 :)

numberNumber1 = input("Please enter 1st number.")
numberNumber2 = input("Please enter the 2nd number.")
numberNumber3 = input("3rd please")
numberNumber4 = input("4th now")
numberNumber5 = input("5th finally")

def mean (operation) :
operation = (numberNumber1/5 + numberNumber2/5 + numberNumber3/5 + numberNumber4/5 + numberNumber5/5)
final = "The mean of these 5 numbers is " + operation
print (final)
return ;

4 个答案:

答案 0 :(得分:0)

首先你的缩进看起来不对劲。 那么你不想让operation作为参数,因为你在计算它,参数应该是你的数字。或者可能没有,而是将您的数字输入放在您的函数中(并且您需要在定义它之后调用它)。最后在python中没有;(并且这个return语句不是必需的,尽管也不是问题)。

答案 1 :(得分:0)

  1. 你的缩进是错误的。

  2. 你实际上从未调用过这个函数。

  3. input返回一个字符串,因此您必须将数字转换为float。

  4. 由于operation是一个浮点数,因此您不能简单地将其连接以创建final字符串。

  5. 您的代码基本上应该更改为:

    numberNumber1 = float(input("Please enter 1st number."))
    numberNumber2 = float(input("Please enter the 2nd number."))
    numberNumber3 = float(input("3rd please"))
    numberNumber4 = float(input("4th now"))
    numberNumber5 = float(input("5th finally"))
    
    def mean(numberNumber1, numberNumber2, numberNumber3, numberNumber4, numberNumber5):
        operation = (numberNumber1 + numberNumber2 + numberNumber3 + 
                     numberNumber4 + numberNumber5) / 5
        final = "The mean of these 5 numbers is {}".format(operation)
        print(final)
    
    mean(numberNumber1, numberNumber2, numberNumber3, numberNumber4, numberNumber5)
    

答案 2 :(得分:0)

这里有几个错误是修复。

numberNumber1 = float(input("Please enter 1st number: "))
numberNumber2 = float(input("Please enter the 2nd number: "))
numberNumber3 = float(input("3rd please: "))
numberNumber4 = float(input("4th now: "))
numberNumber5 = float(input("5th finally: "))

def mean(numberNumber1 ,numberNumber2,numberNumber3,numberNumber4,numberNumber5):
    # Parameters are not needed but in case you change variables now this won't break the function
    mean = numberNumber1 + numberNumber2 + numberNumber3 + numberNumber4 + numberNumber5
    mean = mean / 5
    print("The mean of these numbers is: " + str(mean))

mean(numberNumber1,numberNumber2,numberNumber3,numberNumber4,numberNumber5);

错误是:

  • 您从未调用过该功能
  • 功能错误缩进
  • Python不能将int / float转换为字符串而不会被告知这样做会导致错误

答案 3 :(得分:0)

numberNumber1 = input("Please enter 1st number.")
numberNumber2 = input("Please enter the 2nd number.")
numberNumber3 = input("3rd please")
numberNumber4 = input("4th now")
numberNumber5 = input("5th finally")

def mean() :
    operation = (numberNumber1/5 + numberNumber2/5 + numberNumber3/5 + \
    numberNumber4/5 + numberNumber5/5)
    print("The mean of these 5 numbers is %s" % operation)
    return operation;
print(mean())