Python Forum
using output of one function into another function
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
using output of one function into another function
#1
Write two functions, one called addit and one called mult. addit takes one number as an input and adds 5. mult takes one number as an input, and multiplies that input by whatever is returned by addit
Reply
#2
Thank you for sharing your task, the forum can assist you on parts your are stuck with but cannot do your task for you.
When you get stuck, please post a minimal code sample (in python code tags) for the specific part your stuck on, explain what you expect to happen and what is actually happening and any errors received in error tags.
If someone can they will then assist you.
Reply
#3
Where´s your code? What´ve you tried so far?
Reply
#4
sorry for asking this direct question

i tried the given code but get an namerror' Error: NameError: name 'num' is not defined,
def addit(num):
    return num+5
    
def mult(b):
    y=b*addit(num)
    return y
Reply
#5
You should think along those lines:

>>> def my_int(num): 
...     return int(num)
...
>>> def my_calc(num):
...     return num ** num + num
...
>>> my_calc(my_int('5'))
3130
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#6
Hi,

that's correct, line 5 gives a NameError, as num is unknow there. The key word is: scoping. num is simply unknown in the local scope of mult. If you need num within the funciton, pass it as an argument.

Regards, noisefloor
Reply
#7
@noise floor
can you please correct my code
Reply
#8
Hi,

@ibaad1406: actually I could, but I don't. This is not a "fullfil my wish" forum. We can guide you to the solution, but YOU have to work out the solutions yourself. Otherwise, you won't learn anything.

I gave you the solution already in my previous post, you just have to cast it into code. In case it doesn't work, show your new code, describe your problem, and you will get further assistance.

Regards, noisefloor
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  The function of double underscore back and front in a class function name? Pedroski55 9 605 Feb-19-2024, 03:51 PM
Last Post: deanhystad
  problem in output of a function akbarza 9 1,163 Sep-29-2023, 11:13 AM
Last Post: snippsat
  How to print the output of a defined function bshoushtarian 4 1,278 Sep-08-2022, 01:44 PM
Last Post: deanhystad
  Exit function from nested function based on user input Turtle 5 2,889 Oct-10-2021, 12:55 AM
Last Post: Turtle
  output correction using print() function afefDXCTN 3 11,047 Sep-18-2021, 06:57 PM
Last Post: Sky_Mx
  python prints none in function output chairmanme0wme0w 3 2,201 Jul-07-2021, 05:18 PM
Last Post: deanhystad
Question Stopping a parent function from a nested function? wallgraffiti 1 3,656 May-02-2021, 12:21 PM
Last Post: Gribouillis
Question exiting the outer function from the inner function banidjamali 3 3,512 Feb-27-2021, 09:47 AM
Last Post: banidjamali
  print function output wrong with strings. mposwal 5 3,091 Feb-12-2021, 09:04 AM
Last Post: DPaul
  Output with none, print(x) in function Vidar567 3 2,491 Nov-24-2020, 05:40 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

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