How to use a for loop to print a list of salespeople with their individual sales

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Reggraw

    #1

    How to use a for loop to print a list of salespeople with their individual sales

    My program is supposed to output a list of users who input their sales name along with their sales amount and then print out a display. I am using a nested for loop to read through the list of salespeople along with their sales amounts. The problem is that my for loops keep printing out the salesperson along with the sales of other users...

    Code:
    for sale in range(len(salesAmounts)):
        for name in range(len(newSalesPerson)):
            label = str(1 + name) + '.'
            print 'Sales Person: ', label + newSalesPerson[name] + ' Sales: ',  salesAmounts[sale]
    This is the output:

    Enter Sales Person: (Press ENTER to quit) reg
    Enter the sales amount: 600
    Enter Sales Person: (Press ENTER to quit) tee
    Enter the sales amount: 300
    Enter Sales Person: (Press ENTER to quit) mark
    Enter the sales amount: 200
    Enter Sales Person: (Press ENTER to quit)
    =============== =============== =============
    =============== =============== =============
    Sales Person: 1.reg Sales: 600
    Sales Person: 2.tee Sales: 600
    Sales Person: 3.mark Sales: 600
    Sales Person: 1.reg Sales: 300
    Sales Person: 2.tee Sales: 300
    Sales Person: 3.mark Sales: 300
    Sales Person: 1.reg Sales: 200
    Sales Person: 2.tee Sales: 200
    Sales Person: 3.mark Sales: 200
  • bvdet
    Recognized Expert Specialist
    • Oct 2006
    • 2851

    #2
    Try something like this:
    Code:
    for i, name in enumerate(newSalesPerson):
        print 'Sales Person: %s  Sales: %s', % (name, salesAmounts[i])
    There should not be a nested loop.

    Comment

    Working...