By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,089 Members | 2,432 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,089 IT Pros & Developers. It's quick & easy.

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

P: n/a
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...

Expand|Select|Wrap|Line Numbers
  1. for sale in range(len(salesAmounts)):
  2.     for name in range(len(newSalesPerson)):
  3.         label = str(1 + name) + '.'
  4.         print 'Sales Person: ', label + newSalesPerson[name] + ' Sales: ',  salesAmounts[sale]
  5.  
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
Oct 17 '10 #1
Share this Question
Share on Google+
1 Reply


bvdet
Expert Mod 2.5K+
P: 2,851
Try something like this:
Expand|Select|Wrap|Line Numbers
  1. for i, name in enumerate(newSalesPerson):
  2.     print 'Sales Person: %s  Sales: %s', % (name, salesAmounts[i])
There should not be a nested loop.
Oct 18 '10 #2

Post your reply

Sign in to post your reply or Sign up for a free account.