0

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.

bruno desthuilliers
  • 75,974
  • 6
  • 88
  • 118
  • yes, I think when I am entering a number where state name should have been entered, it is considering it as a string, How do I make sure that only text should be entered, it could be something mentioned in the dictionary or anything else eg - "abc" as I am giving a default value if the state doesn't exist. It should not allow "456" as input. I would appreciate if you can improve the code if you think it is not optimized. – Shivam Thaman Jul 29 '19 at 06:24
  • see this: https://stackoverflow.com/questions/20449427/how-can-i-read-inputs-as-numbers – gregory Jul 29 '19 at 06:33
  • Possible duplicate of [How can I read inputs as numbers?](https://stackoverflow.com/questions/20449427/how-can-i-read-inputs-as-numbers) – gregory Jul 29 '19 at 06:33
  • Possible duplicate of [input string only](https://stackoverflow.com/questions/35589938/input-string-only) – Sai Jul 29 '19 at 06:36

3 Answers3

0

As documented, input (raw_input in Python2.x) always returns a string:

The function then reads a line from input, converts it to a string (stripping a trailing newline), and returns that.

If your need really was to check that state is not the string representation of a numeric (ie "42"), you could use one of the various strings methods like .is_digit() or (for more complex patterns) a regular expression.

BUT actually, what you want to test is not whether this string is "123" or "whatever", but whether it matches a key in your states_tax_rates dict - which is a totally different issue, and one that's quite easy to solve since trying to get an inexistant key from a dict will raise a KeyError:

>>> state_tax_rates = {"nyc" : 0.11, "sfo" : 0.08, "miami" : 0.06}
>>> state_tax_rates["wot"]
Traceback (most recent call last):
  File "<console>", line 1, in <module>
KeyError: 'wot'

so you just have to catch this exception - or check for it's existance beforehand if you prefer:

state = input("Enter state : ")
if state in state_tax_rates:
   # proceed
else:
    print("sorry, {} is not a valid state".format(state))
bruno desthuilliers
  • 75,974
  • 6
  • 88
  • 118
-1

You can do something like this

if(type(variable) is str):
  print('it is string')
else:
  print('It is not string')
  break
halfer
  • 19,824
  • 17
  • 99
  • 186
Jarvis101
  • 9
  • 2
-1

you should use isinstance() function

if isinstance(state,str): //do else: exit()