gpt4 book ai didi

python - python 中并行数组的初学者

转载 作者:行者123 更新时间:2023-11-28 19:19:15 25 4
gpt4 key购买 nike

我已经坚持了将近一个星期的程序。我想做的是创建一个程序,在两个并行数组(姓名和销售额)中输入销售人员的姓名和他们当月的总销售额,并确定哪个销售人员的销售额最大(最大)

Names = [" "]*3
Sales = [0]*3
Index = 0
Max = 0

K = 0

Names[K] = input("Enter salesperson's name and 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 for the month: ",Max)
print("Salesperson: ",(Names[Index]))

它不会提示用户 3 次输入姓名和薪水,而是只询问一次,我收到此错误:

Enter salesperson's name and 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

最佳答案

我建议不要使用“平行数组”,而是将名称和销售数字放入 dictionary 中.这是您需要的内容:

totals = {}
name = input("enter name: ")
while name:
sales = int(input("enter sales: "))
totals[name] = sales
name = input("enter name: ")

此代码还将继续接受新名称,直到您输入空白名称。

从这里你可能应该使用 pythons Counter像这样上课:

# move this import to the top of the file
from collections import Counter

c = Counter(totals)
max_seller = max(c)
print("Max sales for the month", totals[max_seller])
print("Salesperson:", max_seller)

关于python - python 中并行数组的初学者,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29178861/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com