Python Forum
writing user exceptions
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
writing user exceptions
#1
hi guys. I would like to know how to reject invalid user inputs, or write exceptions in this loop. Ive never done it in a for loop, but I have to use one.

lowTaxStates=['California','Nevada', 'Arizona', 'Washington']
for i in range(6):
    employeeName= input('Enter name of employee: ')
    employeeSalary=float(input('Enter yearly salary of employee: '))
    employeeState= input('Enter full name of the state of residence for employee: ')
    if employeeSalary>100000.00:
        federalTaxes=0.20*employeeSalary
    else:
        federalTaxes=0.15*employeeSalary
    if employeeState in lowTaxStates:
        stateTaxes= 0.10*employeeSalary
    else:
        stateTaxes=0.12*employeeSalary
    netSalary=employeeSalary-federalTaxes-stateTaxes
    print('Net salary for employee is :', netSalary )
    print("""
    """)
Reply
#2
It looks like you're already doing some validation:
(Oct-04-2018, 07:05 PM)ccm1776 Wrote:
    if employeeState in lowTaxStates:
        stateTaxes= 0.10*employeeSalary
    else:
        stateTaxes=0.12*employeeSalary
The only thing you don't check first, is the salary. In that case, try a while loop to keep asking until a valid salary is given. Check this out:
>>> valid = False
>>> while not valid:
...   salary = input("Employee's salary: ")
...   try:
...     salary = float(salary)
...     valid = True
...   except ValueError:
...     print("That's not a valid value for salary.  Please try again.")
...
Employee's salary: fish
That's not a valid value for salary.  Please try again.
Employee's salary: $43,000.50
That's not a valid value for salary.  Please try again.
Employee's salary: 43000.50
>>> salary
43000.5
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Calculator exceptions HereweareSwole 5 2,861 Jul-03-2021, 08:08 AM
Last Post: ibreeden
  Writing a function that changes its answer based on user input SirRavenclaw 2 2,827 Dec-21-2019, 09:46 PM
Last Post: Clunk_Head

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020