[Tutor] Help - 2nd validator won't work

Lea Parker lea-parker at bigpond.com
Sat Apr 16 05:33:28 CEST 2011


Hello

 

I now need to get the validator to work on my expense input. If the user
puts in a negative I want an error to come up. I have managed it for the
budget input but cannot seem to get it to work for the expense.

 

Thanks in advance again for your  wonderful help.

 

 

 

"""This program is to calculate if the user is over or under budget

for the month"""

 

 

def main():

  

    # Create an accumulator

    total_expense = 0.00

   

 

    # Ask user for the monthly budget

    budget = float(raw_input('Enter the amount of your budget for the month:
'))

    # Validation variable for budget

    while budget <0:

        print 'ERROR: the budget cannot be a negative amount'

        budget = float(raw_input('Enter the correct budget for the month:
'))

    

 

    # Ask user for expense

    expense = float(raw_input('Enter your first expense '))

    total_expense += expense

    

    # Continue processing as long as the user does not enter 0

    while expense != 0:

 

        #Get another expense

        expense = float(raw_input('Enter expense or 0 to finish '))

        total_expense += expense

       

        

    # Validation variable for expense

        while expense <0:

            print 'ERROR: the budget cannot be a negative amount'

            expense = float(raw_input('Enter the correct budget for the
month: '))

            total_expense += expense

    

        

    #Calculate surplus

    budget_difference = budget - total_expense

 

    #Display results

    print 'Your total expenses for the month ', total_expense

    if budget_difference>=0:

        print 'You are under budget by ', budget_difference, 'dollars.'

    else:

        print 'You are over budget by ', budget_difference, 'dollars.' 

 

# Call the main function.

main()

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20110416/fbe54b30/attachment.html>


More information about the Tutor mailing list