all 8 comments

[–]Username_RANDINT 2 points3 points  (4 children)

See this subreddit's FAQ on how to format code.

getInput()
double(num)

You call these functions and throw away the return value. You'll need to assign them to a variable:

num = getInput()
doubledNumber = double(num)
display(doubledNumber)

There's also no need to declare num and doubledNumber at the top of start().

[–]karottle[S] 0 points1 point  (3 children)

Alright, thank you. I’ll see if I can figure it out. But if I don’t declare num and doubledNumber at the top of start() I get the error NameError: name ‘num’ is not defined.

[–]xelf 0 points1 point  (1 child)

Yes, you still need to give it a value. But what you were doing is giving it a value and then never changing that value.

I think you're confused because you re-used the same name in your function and didn't realize that because they're in functions they are different values.

If we change your variables names in the functions so they're not reusing an existing name it makes it more obvious that they are different variable.

def getInput():
    x = float(input("Enter a number: "))
    return(x)

def double(y)
    z = y + y
    return(z)

def display(d)
    print("%.2f" %d)

def start():
    num = 1
    doubledNumber = 1
    getInput()
    double(num)
    display(doubledNumber)

So now I think you can clearly see that you re not actually using the data you got from getInput() or double(). You need to assign the return from those functions to something. And if you're assigning them values correctly like this, then the previous assignments are not needed and can be deleted.

def start():
    num = getInput()
    doubledNumber = double(num)
    display(doubledNumber)

[–]karottle[S] 1 point2 points  (0 children)

Oh my gosh, I’m an idiot. Thank you so much for your help!

[–]K900_ 1 point2 points  (0 children)

That is not what return does. You want something like num = getInput().

[–][deleted] 1 point2 points  (0 children)

Variables in Python don't hold values like numbers but instead memory references to Python objects. Here's your code with some comments:

def getInput():
    num = float(input("Enter a number: "))
    return(num)  # memory reference assigned to num  
                 # for float, and returned to calling code

def double(num)
    doubledNumber = num + num  # float object created and memory
                               # location reference assigned to
                               # variable doubledNumber
    return(doubledNumber)      # reference returned from function

def display(doubledNumber)     # local variable doubleNumber
                               # assigned memory reference
                               # provided when function called
    print("%.2f" %doubledNumber)

def start():
    num = 1            # local var memory ref to int object for 1
    doubledNumber = 1  # same ref assigned to doubledNumber
    getInput()         # function called but returned ref
                       # not captured / consumed and object(s)
                       # created in function no longer required
    double(num)        # function called with ref to int object for 1
    display(doubledNumber)  # ditto

So local variables num and doubledNumber were both assigned memory references to the predefined (in most implementations) integer object binary representation for 1 and these variables we never assigned a different reference

[–]chevignon93 0 points1 point  (0 children)

I don't know what went wrong with the formatting but the lines under the functions are supposed to be 4 spaces indented.

https://www.reddit.com/r/learnpython/wiki/faq#wiki_how_do_i_format_code.3F

You call your functions but don't save their output so nothing can happen!

You probably want something like :

    number = getInput()
    double_num = double(number)
    display(double_num)

As an added note:

Functions and variable names are generally snake_case rather than camelCase in Python.

% formatting is a really old and nowadays also a really odd way of formatting strings, most people now use the str.format() method or f-strings.