0

Just started using functions in python 2.7.5, working on assignment for school but was having problems with output of the correct results, I want the user to enter 3 numbers and get the product of them. I can get the sum of 2 numbers but my issue is that I can get the input from the user but the output either doesn't display correctly or I get an error, here is my code this far: (I'm very new to coding so please be as detailed as possible)

#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()

my issue is I need a product function but can't seem to get it correct. if I put an isum3 and define a new function as getProd for example, it doesn't display correctly,

4
  • 2
    Can you show your version of the code that has the getProd function and isum3 defined? Commented Nov 25, 2013 at 17:21
  • I have just added the lines I attempted with the getProd for calculating the product of 3 numbers entered by the user. Commented Nov 25, 2013 at 19:54
  • I think something may have been lost in the editing process. I see that the title has changed, but the code is still the same. Commented Nov 25, 2013 at 20:25
  • my fault, using works wifi at the moment, I added the input/output tags, changed the title because I used the word procedure instead of function, the flow chart tool we use calls python functions procedures. Commented Nov 25, 2013 at 20:38

1 Answer 1

1

Your getProd is a good start, but you have to return something for it to be useful.

def getProd(n1,n2,n3):
    itotal = n1 * n2 * n3 
    return itotal

displayData isn't suitable for displaying product data, since it mentions a "total" and has only two variables. You could write another method with similar behavior, however:

def displayProductData(first, second, third, total):
    print("The first value is = ", first);
    print("The second value is = ", second);
    print("The third value is = ", third)
    print("The Total is = ", total);

Lastly, you need to update your main so that it properly uses the new method.

def main():
    isum1 = 0
    isum2 = 0
    isum3 = 0 

    isum1 = getValue()
    isum2 = getValue()
    isum3 = getValue() 
    itotal = getProd(isum1, isum2, isum3)

    displayProductData(isum1, isum2, isum3, itotal)

Other Miscellaneous Improvements

The Python style guide suggests using lower_case_with_underscores when naming variables and functions, rather than mixedCase.

def get_value():
    #code

def get_sum():
    #code

def get_prod()

Within functions, it is possible to directly return an expression, without having to assign it to a variable first.

def get_sum(first, second):
    return first + second

def get_prod(n1,n2,n3):
    return n1 * n2 * n3

In main, you write that you "need to initialize the needed variables", but in fact you do not have to do that at the beginning of a function! You can assign to a variable wherever you want. Just don't attempt to use a variable that hasn't been defined yet.

def main():
    isum1 = getValue()
    isum2 = getValue()
    isum3 = getValue() 
    itotal = getProd(isum1, isum2, isum3)

    displayProductData(isum1, isum2, isum3, itotal)

If you are using Python 2 and not Python 3, then print doesn't require parentheses. In fact, the output may look better as a result, since without parentheses it won't interpret your expression as a tuple.

def display_product_data(first, second, third, total):
    print "The first value is = ", first
    print "The second value is = ", second
    print "The third value is = ", third
    print "The Total is = ", total

The output used to be:

('The first value is = ', 2.0)
('The second value is = ', 3.0)
('The third value is = ', 4.0)
('The Total is = ', 24.0)

Now it is:

The first value is =  2.0
The second value is =  3.0
The third value is =  4.0
The Total is =  24.0    

You don't need to use semicolons. They're just there to make users comfortable when coming from other languages. And to delimit multiple statements on one line, but that's not very common.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.