user6072696
user6072696

Reputation: 13

Python return troubles

Can someone please explain what I'm doing wrong?

My code come's up as x not defined. What i'm trying to do is get the result of x in add1 and pass it to the doub function. I have searched and read up on this as much as possible, I know I am missing something simply so please point me in the right direction.

def main():
    value = (int)(input('Enter a number '))
    add1(value)
    doub(x)

def add1(value):
    x = value + 1
    return x


def doub (x):
    return x*2

main()

Upvotes: 0

Views: 56

Answers (2)

Daniel Roseman
Daniel Roseman

Reputation: 599490

x only exists within the add1 function. All your main function knows is the value that's returned from calling add1, not the name of the variable it was previously stored in. You need to assign that value to a variable, and pass that into doub:

result = add1(value)
doub(result)

Also note, Python is not C; there's no such thing as typecasting. int is a function that you call on a value:

value = int(input('Enter a number '))

Upvotes: 2

linusg
linusg

Reputation: 6429

Try this:

def main():
    value = int(input('Enter a number '))
    #This is more pythonic; use the int() function instead of (int)         
    doub(add1(value))

def add1(value):
    x = value + 1
    return x


def doub (x):
    return x*2

main()

Upvotes: 0

Related Questions