Previous Lecture Complete and continue  

  Square a Number

Notes to Help You

  • Functions start with def
  • Don't forget your colons
  • Always return in functions rather than print!

Example Problem.
Write a function add_two that takes in two numbers as input and returns their sum.

24 comments
Aayush Rattan
def square_number(a):
return(a*a)
Denis
Very good tutorial! I just have one question;
How does Python know that
def test_square_number is a test of the square_number function and not a new function itself? Is it because assertions are running within it?

I also thought that 'test' might be a Python keyword but it isn't and I have yet to find a good explanation online.
Joshua Morgan
I'm a noob but I think you're right about the assertions. I think the function "test_square_number" is just looking at the assertions below it which has a "pointer" to the function "square_number". The stand alone "test_square_number" at the very end of the code is running that function. It is checking to see if your "number" equals one of the assertions. If you run the program with any number that does not equal the assertions it gives an assertion error.
Denis
Perfect, that sounds like a good explanation. Thanks Joshua!
Jackson AIken
https://repl.it/Fqkn/0
Aditi Deepak
num = input("Enter your number : ")
a = int(num)
def square_number(a):
return a*a
print(square_number(a))
Jackson AIken
That's what I thought to do at first but with a variable, you would have to open and close the program every time you wanna run it. Also, instead of doing a*a just do a ** 2 that means its taking the input and squaring it. Also when you did return a*a, thats right but you also did the print, you dont need that, the return will print itself
Dachi
def square(a):
return a* a
print("You Code Is Correct")


print(square(10))
Aman Tyagi
Hey guys.. check out this little game that i made.
https://repl.it/FRpp/7
Tristan Lee
Nice!! That was pretty fun. It is pretty big and could probably be a little smaller with use of more functions (Like for ex.

def error():
print("Error. Invalid input")

so you don't need to keep typing that out all over again [I'm just lazy :P])

Nice game great job!
Martin
i'll tell you guys a joke, when Qazi gave us this exercise my brain immediately sprung into overdrive and the only thing I could think of, was how to create a code that'll work out the square of a number. So I wasted an entire day breaking my brain "Cause I'm still a rookie" just to try and figure out how to write a code. I eventually gave up and just watched the solution video. (GLAD TO SEE ITS AS EASY AS ** 2 TO SQUARE A NUMBER) Luckily I had fun.
Amal Abdiweli
def square_num(num):
return num*num
print(square_number(3))
Vincent Houckham
def square_number(squaredNumber):
squaredNumber = squaredNumber * squaredNumber
return squaredNumber
poonam ahir
def square_num(number):
squared=number*number
return squared

print(square_num(4))
Adex Elizy
https://repl.it/FBEz/5
it's not printing your code is correct
ANURAG SHARMA
because you haven't called the test_square_number function
Phuc Le
def square_number(number):
squared=number*number
return squared
Zsolt Bisztray
https://repl.it/FBEz/2
ashish dagar
def square_number(number):
return number**2

print(square_number(number))
Sebastian Gomez
https://repl.it/FBEz/1
Thomas Liebig
def square_number(a):
return a * a
Tao Burga
huhhhh I've been trying for about an hour and when i gave up and saw the solution it was just as i had done it its just nothing would happen. i kept getting this:
IndentationError: unindent does not match any outer indentation level on line 25 test_square_number() ^ in main.py
heres my link in case you want to check it out: https://trinket.io/python/0c71971445
Tara Fahey
Six days late but eh. Your code wasn't wrong, per say, the only problem was that the last line ( the line that tests your code) had a single space throwing off the entire program. Simply get rid of that space and it'll work.

riya
def square(number):
return number*number
print (square(2))

for i in range(12):
print(square(i))