Computer Science
Predict the output of the following code.
def code (n) :
if n==0:
print ( 'Finally' )
else:
print (n)
print (n-3)
code (10)
Python
Python Functions
3 Likes
Answer
10
7
Working
This code defines a function code(n)
that takes a single argument n
. Inside the function, there's a conditional statement that checks if n
is equal to 0. If n
is indeed 0, it prints the string "Finally". Otherwise, it prints the value of n
and then calculates and prints n-3. When the function code(10)
is called, it passes the argument 10 to the function. Since 10 is not equal to 0, the function first prints 10 and then calculates and prints 10-3, which is 7.
Answered By
2 Likes
Related Questions
Predict the output of the following code.
def code (n) : if n==0: print('Finally') else: print(n) code (2-2) code (15)
Predict the output of the following code.
def code(n): if n == 0: print(' Finally') else: print (n) code (10)
Predict the output of the following code:
def express (x, n) : if n == 0: return 1 elif n % 2 == 0: return express (x*x, n/2) else: return x * express (x, n-1) express (2, 5)
Consider the following Python function that uses recursion.
def check (n) : if n <=1: return True elif n % 2 == 0: return check (n/2) else: return check (n/1)
What is the value returned by check(8) ?