I want to run a program in a way that checks if the input from the user is of string type, if yes, the function should be called else, the further code execution should stop.
I've tried using if-else condition and try-except. I've also tried to write type(state) == str as well as type(state) == "str"
from babel.numbers import format_currency
state_tax_rates = {"nyc" : 0.11, "sfo" : 0.08, "miami" : 0.06}
def net_income_calculation(state, gross_income):
fedral_tax = 0.1 * gross_income
print("fedral_tax = ", format_currency(fedral_tax, 'USD',
locale = 'en_US'))
state_tax = (state_tax_rates[state] * gross_income if state in
state_tax_rates else 500.00)
print("state_tax = ", format_currency(state_tax, 'USD', locale
= 'en_US'))
total_tax = fedral_tax + state_tax
print("total tax = ",format_currency(total_tax, 'USD', locale
= 'en_US'))
net_income = gross_income - total_tax
print("net_income = ", format_currency(net_income, 'USD',
locale = 'en_US'))
state = input("Enter state : ")
if type(state) == str:
gross_income = float(input("Enter gross_income : "))
net_income_calculation(state, gross_income)
else:
print("state should be a string(English)")
Expected :
Enter state : nyc Enter gross_income : 5000 fedral_tax = $500.00 state_tax = $550.00 total tax = $1,050.00 net_income = $3,950.00
Actual :
(if I do, type(state) == str)
Enter state : 456025 Enter gross_income : 411223 fedral_tax = $41,122.30 state_tax = $500.00 total tax = $41,622.30 net_income = $369,600.70
Here it should have stopped the execution because I entered a number where the name of the state has to be entered.
(if I do type(state) == "str" )
Enter state : nyc state should be a string(English)
Here it should have allowed execution.
Nothing happens on using try-except.