Beginner with parallel arrays in python -
i've been stuck program week. i'm trying create program inputs names of salespersons , total sales month in 2 parallel arrays (names , sales) , determine salesperson has greatest sales(max)
names = [" "]*3 sales = [0]*3 index = 0 max = 0 k = 0 names[k] = input("enter salesperson's name , monthly sales: (to exit enter * or 0)") sales[k] = int(input("enter monthly sales:")) while (names[k] !="*"): if sales[k] > max : index = k max = sales[index] k = k + 1 print("max sales month: ",max) print("salesperson: ",(names[index]))
it doesn't prompt user 3 times name , salary instead asks once , error:
enter salesperson's name , monthly sales: (to exit enter * or 0)jon enter monthly sales:3 traceback (most recent call last): file "c:\users\user\downloads\sales.py", line 18, in <module> while (names[k] !="*"): indexerror: list index out of range
i suggest instead of having 'parallel arrays' put names , sales figures dictionary. here looks need:
totals = {} name = input("enter name: ") while name: sales = int(input("enter sales: ")) totals[name] = sales name = input("enter name: ")
this code continue accepting new names until enter blank name.
from here should use pythons counter
class this:
# move import top of file collections import counter c = counter(totals) max_seller = max(c) print("max sales month", totals[max_seller]) print("salesperson:", max_seller)
Comments
Post a Comment