Write a python function definition for a function, get_tax that takes as a param
ID: 3909497 • Letter: W
Question
Write a python function definition for a function, get_tax that takes as a parameter an optional tax rate. If the tax rate is not specified, it defaults to 0.1 (10%).
The function prompts the user for a purchase amount. If the user enters a valid number, the function returns the computed tax on that amount. Otherwise the function returns 0.
Note that you do not have to round any value before returning it. Your function must return the value - not print it.
You may test your function as follows.
Test case 1:
print(get_tax()) # assuming the user enters 100 at the prompt, 10.0 is printed
Please enter an amount in $: 100
10.0
Test case 2:
print(get_tax(0.06)) # assuming the user enters 17.5 at the prompt, 1.05 is printed
Please enter an amount in $: 17.5
1.05
Test case 3:
print(get_tax()) # assuming the user enters hello at the prompt, 0.0 is printed
Please enter an amount in $: hello
0.0
Test case 4:
print(get_tax(0.09)) # assuming the user enters 1.2.3 at the prompt, 0.0 is printed
Please enter an amount in $: 1.2.3
0.0
Test case 5:
print(get_tax(0.08)) # assuming the user enters -100 at the prompt, -8.0 is printed
Please enter an amount in $: -100
-8.0
Explanation / Answer
def get_tax(rate=0.1): try: amount = float(input('Please enter an amount in $: ')) except: amount = 0 return amount * rate print(get_tax()) print(get_tax(0.06)) print(get_tax()) print(get_tax(0.09)) print(get_tax(0.08))
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.