Write power(base, exp=2), which returns base raised to exp. If only one number is given, square it.
What to build
A function whose second parameter is optional.
Requirements
- Write
power(base, exp=2). - Read one or two numbers, and call the function accordingly.
Input
One line with one or two whole numbers: base, and optionally exp (0 to 30).
Output
The value of base ** exp.
Constraints
exp is from 0 to 30.
Example
Input:
5
Output:
25
Why it works
The default means that power(5) and power(5, 2) are the same call, so the common case stays short.
Examples
Example 1 · Input
5
Expected output
25
Example 2 · Input
2 10
Expected output
1024
Prompts inside input() are ignored when checking, so input("n: ") is fine.
Hints
Hint 1
exp=2 in the parameter list makes 2 the default.
Hint 2
base ** exp raises base to the power exp.
Solution
Show one correct solution (try it yourself first)
def power(base, exp=2):
return base ** exp
parts = input().split()
if len(parts) == 1:
print(power(int(parts[0])))
else:
print(power(int(parts[0]), int(parts[1])))