John MacPhee
John MacPhee

Reputation: 11

I need to figure out how to create a user defined function to calculate the average of three numbers

I am taking an introduction to Python course and cannot figure out how to do anything correctly. User defined functions are only the latest in a line of issues I am having with this class.

I need to make a user defined function that will calculate the average of three numbers, and then I need to call it in my code. Please help me.

# This program needs to calculate the average of three numbers using my user defined function

import math

def average(num1,num2,num3):
      total=(num1+num2+num3)
      float(total)
      favg=total/3
      float(favg)
      print("The average is:",favg)
      return(average)
  

###########################Main Program#########################################
num1= input("Please enter the first number:")
float(num1)
num2=input("Please enter the second number:")
float(num2)
num3=input("Please enter the third number:")
float(num3)
print(average(num1, num2, num3))            

Upvotes: 1

Views: 1427

Answers (2)

gsb-eng
gsb-eng

Reputation: 1209

Try this logic..

def average(num1,num2,num3):
    total = num1 + num2 + num3
    favg = float(total)/float(3)
    print "The average is:",favg
    return favg

average(1, 22, 33)

Output:

The average is: 18.6666666667
18.666666666666668

Upvotes: 2

henryr
henryr

Reputation: 169

You don't get the float numbers for the inputs, you can do this

num1=float(input("Please enter the first number:"))
num2=float(input("Please enter the second number:"))
num3=float(input("Please enter the third number:"))
print(average(num1, num2, num3))

Or this

print(average(float(num1), float(num2), float(num3)))

Upvotes: 1

Related Questions