+ 2
Write a function that accepts a number as argument and returns the square of the number. For example if the number passed to the
My code is working but due to the instructions I get the error which includes the function arguments. Please help! n=int(input()) def square_num(): a = n**2 my_result = a return my_result print(square_num())
6 Antworten
+ 4
Save your code in code playground and share it's link here.
You've used n inside your function, but where did you declare it inside the function? Your function is a seperate block that doesn't know about the variables and functions outside of it. So you need to pass in the variable n so that your function knows what n is. It'll something like the below snippet:
...
def square_num(n):
...
Hope you understood the explanation. Feel free to ask doubts if any
+ 2
What are the instructions?
Use description place to write full question??
+ 2
# yes, code works but there expected clean code is :
def square_num(n):
a = n**2
my_result = a
return my_result
n = int( input() )
print( square_num(n) )
+ 1
The whole question is Write a function that accepts a number as argument and returns the square of the number. For example if the number passed to the function is 5 then your function should return 25
+ 1
every syntax matters! my first practice with function. got my mistake,
thank you for your support! appreciate it!
0
#by taking input via argument
def square_num(n):
a = n**2
my_result = a
return my_result
print(square_num(n=int(input())))