Python Forum
Need help with a function that calls other functions. - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Need help with a function that calls other functions. (/thread-21447.html)



Need help with a function that calls other functions. - skurrtboi - Sep-30-2019

Im pretty new at programming, but atm im trying to make a function that takes two user inputs and use the input on previously made functions.
I only need help with the last part (part 4).

(BTW some of the words are in norwegian so here are some translated word to make it easier to understand if you need it): addisjon - addition, tall - number, subtraksjon - subtraction, divisjon - division, retur - return, verdi - value, tommer - inches, beregninger - calculations, Skriv inn - write.

THANK YOU IN ADVICE!
""" 1"""

def addisjon (tall1, tall2):
    return tall1 + tall2

returverdi = addisjon (5, 5)
assert addisjon (5,5)==10
assert addisjon (-10, -10)==-20
assert addisjon (-7, 10)==3
print (returverdi)

""" 2 """

def subtraksjon (tall1, tall2):
    return tall1 - tall2


returverdi = subtraksjon (15, 6)
assert subtraksjon (15,15)==-0
assert subtraksjon (-30, 20)==-50
assert subtraksjon (-20, -10)==-10
print (returverdi)

def divisjon (tall1, tall2):
    return tall1 / tall2


returverdi = divisjon (50, 5)
assert divisjon (30, 2)==15
assert divisjon (-50, 3)==-16.666666666666668
assert divisjon (-30, -6)==5
print (returverdi)

""" 3 """

def tommerTilCm (antallTommer):
    assert antallTommer > 0
    return antallTommer * (2.54)

returverdi = tommerTilCm (12)
print (returverdi), "Cm"

""" 4 """

def skrivBeregninger():
    a=input ("Skriv inn tall 1: ")
    b=input ("Skriv inn tall 2: ")
    for a and b in skrivBeregninger():
        print (addisjon)
        print (subtraksjon)
        print (divisjon)
print (skrivBeregninger())



RE: Need help with a function that calls other functions. - ichabod801 - Sep-30-2019

Your last function is calling itself on line 48. This will go on forever, since there is nothing to stop the function between the start of the function and it calling itself.

It's not sure what you are trying to do here. Do you want the last function to add, subtract, and divide a and b? If so, you don't need a loop, but you need to call those functions with a and b as parameters (just like you do in all of your test code).

In the future, please describe the error you are having, including the full text of any error messages. It makes it much easier on our end.


RE: Need help with a function that calls other functions. - skurrtboi - Sep-30-2019

I see. Im spesifically trying to not take parameters on this last part, but instead take two user inputs and run those two numbers through addition, subtraction and division and then print the outcome of each function. Sorry, first time posting here!


RE: Need help with a function that calls other functions. - ichabod801 - Sep-30-2019

Okay, but you need to pass those two user inputs as parameters to the three math functions.


RE: Need help with a function that calls other functions. - stullis - Sep-30-2019

As Ichabod stated, the functions need to have values for their arguments; there's no way around it. However, there are a couple addition items to change to accomplish your goal.

Line 48 won't work. A for loop only works on objects with a __next__() method. This line will raise a NoneType error, most likely. For your desired outcome, you don't need a for loop at all; the variables "a" and "b" are already there.

Lines 49 - 51 will print a memory location instead of the output of the functions. This is due to the functions being referenced but not called. To call them, parentheses are needed afterward along with their argument values.

Line 52 will print "None" because skrivBeregninger() does not return a value. You may want to return all three functions in a tuple from skrivBeregninger().

def skrivBeregninger():
    a=input ("Skriv inn tall 1: ")
    b=input ("Skriv inn tall 2: ")
    return (addisjon(a, b), subtraksjon(a, b), divisjon(a, b))