A little confused

Hello all,

Python noob here, I was bored and thought I would try and make a quick function to square a given number. In short it did’nt work. Any advice? Attached is the screenshot.


Thanks in advance

Hello, @dev1421290795.

First off, to invoke or call a function you have to include the () following its name. The () should include any expected arguments. Your function is expecting an argument since it has a parameter named number. You also have a global variable named number, so you might want to consider renaming one or the other.

Second, there is no need to print() twice. If the function is going to do the printing then simply invoke the function like so: squared(argument). If you would rather have the function return the value, and then print it, you could do this:

number = 7

def squared(num):
    return num * num

print(squared(number)) #prints 49

If you leave the 2 print() statements like you have it, the function will print the result, and then implicitly return None. The print() statement calling the function will then print that result: None.
For example:

number = 7

def squared(num):
    print(num * num) #prints 49

print(squared(number)) #prints None

Output:

49
None

To avoid printing None we just call the function:

number = 7

def squared(num):
    print(num * num) #prints 49

squared(number)

Output:

49

Happy coding!

1 Like

Thank you so much!
I spent only 2mins making the code but over 20mins searching the web for an answer. Your reply really cleared things up.
Thanks again

1 Like

This topic was automatically closed 18 hours after the last reply. New replies are no longer allowed.