0

如何编写文件而不在循环中重复?如何继续将循环写入文件以及如何在每个条件满足后将初始值重置为 0?

def main():
    infile=open("sales.txt","w")
    infilelist=[]
    #call function
    totalsale_each,salesperson=sales(infile,infilelist)

    for salesamount in infilelist:
        infile.write(salesamount)

    infile.close()


def sales(infile, infilelist):
    #set initial
    t_sales=0

    n_sales=int(input("Number of sales"))

    while t_sales<n_sales:

        #increasement
        t_sales+=1


        #for loop
        for s in range(1,n_sales+1):
            totalsale_each=0 #set the acc for total sales by each sales person


            sales_person=input("sales person name:")
            print("sales for sales no. "+str(s)+"by"+sales_person+":")
            infilelist.append(sales_person)               

            for count in range (1,5): #assuming one sales person can sell max and min of 5item per cust
                sales=float(input('sales#' +str(count)+ ':'))

                if sales<=300:
                    t_sales=t_sales+sales
                    totalsale_each=totalsale_each+sales

                if sales>300: #if sales>300, need to change the sales person
                              #that sales person needs to finish serving the rest of that cust's items
                    t_sales=t_sales+sales
                    sales_person=input("another sales pesron")
                    infilelist.append(sales_person)

                infilelist.append(str(totalsale_each)) #to write total sales for each person 
    return totalsale_each,sales_person

main()

这是我的python测试:

Number of sales: 2
sales person name:A
sales for sales no.1 by A:
sales#1:100
sales#2:150
sales#3:350
another sales pesronB
sales#4:200
sales person name:C
sales for sales no.2 by C:
sales#1:200
sales#2:500
another sales pesronD
sales#3:500
another sales pesronE
sales#4:200

我运行后得到的文件是

 A
100.0
250.0
B
250.0
450.0
C
200.0
D
200.0
E
200.0
400.0

但我想要得到的是如下:

A
600
B
200
C
700
D
500
E
200

我该如何纠正?

当销售额超过 300 时询问下一个销售人员后,我无法弄清楚将infilelist.append(sales_person) 和 infilelist.append(totalsale_each)放在哪里。

谢谢

4

1 回答 1

2

如果我理解正确,当销售额超过 300 时,你要写那个人的总销售额,并要求下一个。

所以,你需要做的是,在计算总销售额之后,加上每个人的总销售额,这意味着在循环之后range(5)

另外,当你发现一个销售人员已经卖出了超过 300 件后,你需要停止计算销售额。所以,你需要break在那个时候循环,并正常继续。

这里的问题是在那之后去找下一个销售人员。要么继续循环,它会像输入任何其他名称一样提示,或者您应该以另一种形式请求它,并将其插入数组中,并确保它不会像通常那样请求另一个名称。如果您需要第二个选项,请在评论中说明,第一个选项更简单。

因此,我建议对sales函数进行以下修改:

def sales(infile, infilelist):
    #set initial
    t_sales=0

    n_sales=int(input("Number of sales"))

    while t_sales<n_sales:

        #increasement
        t_sales+=1


        #for loop
        for s in range(1,n_sales+1):
            totalsale_each=0 #set the acc for total sales by each sales person

            # EDIT: this seems useless, so remove it
            #sales_no=s*1
            sales_person=input("sales person name:")
            print("sales for sales no."+str(s)+"by"+sales_person+":")
            infilelist.append(sales_person)               

            for count in range (5): #assuming one sales person can sell only 5item per cust
                sales=float(input('sales#' +str(count)+ ':'))

                if sales<=300:
                    t_sales=t_sales+sales
                    totalsale_each=totalsale_each+sales

                if sales>300: #if sales>300, need to change the sales person
                    t_sales=t_sales+sales
                    # write the last sales_person's total, and ask for another one
                    infilelist.append(str(totalsale_each))
                    sales_person=input("another sales pesron")
                    # add it to the list, after that, everything counts for him
                    infilelist.append(sales_person)
                    totalsale_each=sales
            # EDIT: decrease indentation so that the total is written once, after the loop
            infilelist.append(str(totalsale_each)) #to write total sales for each person 
    return totalsale_each,sales_person

编辑:我编辑了代码:删除了break,写上最后一个的总数,要求另一个销售人员,重置总数计数器,并以他的名义写下所有内容。

另外,关于退货声明的注释(也许你已经知道了):它将返回最后一个销售人员的总销售额和姓名。

但是,我建议重写整个内容,以便将总数保存在字典中,键 = 销售人员的姓名或索引 (sales_no)。之后,您将它们全部写入列表。

编辑 2:如果您注意到,在添加新的 sales_person ( infilelist.append(sales_person)) 之后,我会将销售额添加到totalsales_each. 您想要的似乎相反,因此您应该在添加新的销售人员之前执行此操作,这将为您提供:

if sales>300:
    t_sales=t_sales+sales
    totalsale_each += sales # the last sale counts for the last person
    infilelist.append(str(totalsale_each))

    sales_person=input("another sales pesron")
    infilelist.append(sales_person)
    totalsale_each = 0 # reset the sales for the next person
于 2012-05-19T18:40:53.070 回答