def f(n):
if n == 1:
return 1
return n + f(n-1)
print(f(2))
Answer will be 3
------------------------------------
def f(n):
if n == 1:
return 1
return n + f(n-1)
Answer will be None
when you run this snippet on a compiler it prints out None.
Even if the indentation is corrected you can not have 2 results in one function unless it is separated by an else like below
def get_absolute_value(num):
if num < 0:
return -num
else:
return num
result = get_absolute_value(-5)
print(result) # Output: 5
It's a recursive function that adds up all the numbers from n, n-1, n-2 ,...,1. So, given n=2, and assuming the indentation is correct, the result will be: 2 + 1 = 3.
This section is not available anymore. Please use the main Exam Page.PCAP Exam Questions
Log in to ExamTopics
Sign in:
Community vote distribution
A (35%)
C (25%)
B (20%)
Other
Most Voted
A voting comment increases the vote count for the chosen answer by one.
Upvoting a comment with a selected answer will also increase the vote count towards that answer by one.
So if you see a comment that you already agree with, you can upvote it instead of posting a new comment.
Dave304409
5 days ago46a1b89
6 months agoalopezme
1 year agoblaze056
1 year, 3 months agoTheFivePips
1 year, 4 months agoswyyuen
1 year, 6 months agoemanuelcar990
1 year, 8 months agoowenmagas
1 year, 8 months agoCC_DC
1 year, 9 months agoEllo2023
1 year, 9 months agoEllo2023
1 year, 9 months agoEllo2023
1 year, 9 months agodavid0001
2 years, 2 months agorotimislaw
2 years, 5 months agoRam5678
2 years, 6 months agoJO5H
2 years, 6 months agoalfonsocav1982
2 years, 7 months ago