Python Forum
Need help with a function that calls other functions.
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Need help with a function that calls other functions.
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.

""" 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())
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.
Craig "Ichabod" O'Brien -
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
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!
Okay, but you need to pass those two user inputs as parameters to the three math functions.
Craig "Ichabod" O'Brien -
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
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))

Possibly Related Threads…
Thread Author Replies Views Last Post
  RuntimeError: Optimal parameters not found: Number of calls to function has reached m bntayfur 0 279 Aug-05-2020, 04:41 PM
Last Post: bntayfur
  Run a timer when a functions starts to see how long the function takes to complete Pedroski55 2 393 Apr-19-2020, 06:28 AM
Last Post: Pedroski55
  creating a list of dictionaries from API calls AndrewEnglsh101 5 466 Apr-03-2020, 02:21 PM
Last Post: AndrewEnglsh101
  How to intercept python method calls? DineshBhat 0 513 Jul-15-2019, 03:46 AM
Last Post: DineshBhat
  How to split a string containing function calls? Metalman488 4 987 Oct-27-2018, 06:50 PM
Last Post: Metalman488
  call a function from other functions ... evilcode1 2 962 Sep-05-2018, 09:07 AM
Last Post: evilcode1
  Testing function calls jenselme 1 906 Jul-25-2018, 10:33 AM
Last Post: Larz60+
  argument parser: to execute single function, and subsequent functions also raghu 10 2,240 Mar-12-2018, 06:57 AM
Last Post: wavic
  Function to Continuasly monitor serial port and control other functions ricardons 10 3,414 Mar-04-2018, 10:34 PM
Last Post: Larz60+
  function state between calls Skaperen 5 2,028 Feb-08-2018, 02:20 AM
Last Post: Skaperen

Forum Jump:

User Panel Messages

Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020