Python code problem

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • BSleazy
    Confirmed User
    • Aug 2002
    • 6721

    #1

    Python code problem

    When running this little program, 0 for a sales amount should always deliver the error message. For some reason after I enter some other incorrect values and then try 0, the program allows it and completes. Must be something with the loop but I can't tell. Do I need to group the 2 "while"s together or what? Help please

    Code:
    #Define the main function
    def main():
        # create a variable to control the loop
        keep_going = 'y'
        
        # create a counter for salesperson's
        number_of_salespersons = 0
        
        # process each salesperson's sales
        while keep_going == 'y' or keep_going == 'Y':
    
            # use a function to process each salesperson
            process_salesperson()
    
            number_of_salespersons += 1
    
            # are there more salesperson's?
            keep_going = raw_input('Are there more salespeople? (enter y for yes): ')
    
        # display the total number of salespeople
        print "There were", number_of_salespersons, "salesperson's."
        
        
        
    def process_salesperson():
        # get the salesperson's name
        name = raw_input("What is the salesperson's name? ")
    
        # input the first sale amount
        print 'Enter', name + "'s first sale amount:",
        first_sale_amount = input()
        
        # validate the sale amount is  > $1 and < $25000
        while first_sale_amount < 1:
            print "ERROR: the sale amount must be between $1 and $25000."
            first_sale_amount = input("Please enter a correct sale amount: ")
            
        while first_sale_amount > 25000:
            print "ERROR: the sale amount must be between $1 and $25000."
            first_sale_amount = input("Please enter a correct sale amount: ")
            
        # intialize average, highest, and lowest sale amount
        total_sales = first_sale_amount
        highest_sale = first_sale_amount
        lowest_sale = first_sale_amount
        
        # get the number of sales for this person
        print 'How many sales did', name, 'have?',
        number_of_sales = input()
        
        for number in range(2, number_of_sales + 1):
            # get the sale amount
            print 'Enter', name + "'s amount for sale #" + str(number) + ':', 
            sale_amount = input()
            
            # validate the sale amount is  > $1 and < $25000
            while sale_amount < 1:
                print "ERROR: the sale amount must be between $1 and $25000."
                sale_amount = input("Please enter a correct sale amount: ")
            
            while sale_amount > 25000:
                print "ERROR: the sale amount must be between $1 and $25000."
                sale_amount = input("Please enter a correct sale amount: ")
                
            # accumulate the sales
            total_sales += sale_amount
    
            # check for highest sale
            if sale_amount > highest_sale:
                highest_sale = sale_amount
    
            # check for lowest sale
            elif sale_amount < lowest_sale:
                lowest_sale = sale_amount
                
        # compute average sale
        average_sale = float(total_sales) / number_of_sales
        
        # display the average, highest, and lowest sales
        print
        print name + "'s average sale was: \t $%6.2f" % average_sale
        print name + "'s highest sale was: \t $%6.2f" % highest_sale
        print name + "'s lowest sale was: \t $%6.2f" % lowest_sale
        print
    
    # call the main function
    main()
    icq 156131086
Working...