Python 如何继续将循环写入文件,以及如何在满足每个条件后将初始值重置为0?
如何编写文件而不在循环中重复? 如何继续将循环写入文件,以及如何在满足每个条件后将初始值重置为0Python 如何继续将循环写入文件,以及如何在满足每个条件后将初始值重置为0?,python,loops,for-loop,python-3.x,while-loop,Python,Loops,For Loop,Python 3.x,While Loop,如何编写文件而不在循环中重复? 如何继续将循环写入文件,以及如何在满足每个条件后将初始值重置为0 def main(): infile=open("sales.txt","w") infilelist=[] #call function totalsale_each,salesperson=sales(infile,infilelist) for salesamount in infilelist: infile.write(salesa
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()
我运行后得到的文件是
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时,在询问下一位销售人员后,我不知道在哪里放置infielist.append(销售人员)和infielist.append(总销售人员各)
谢谢你如果我理解正确的话,当销售额超过300美元时,你想写下那个人的总销售额,然后要求下一个 因此,您需要做的是,在计算总销售额后,添加每个人的总销售额,,这意味着在
范围(5)
中循环之后
此外,当你发现一个销售人员的销售额超过300英镑时,你需要停止计算销售额。因此,您需要在此时中断循环,并正常继续
这里的问题是,在那之后,要交给下一个销售人员。您可以继续循环,它会像输入任何其他名称一样提示,或者您应该以另一种形式请求它,并将其插入数组中,并确保它不会像通常那样请求其他名称。如果您需要第二个选项在注释中这样说,那么第一个选项更简单
因此,我建议对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
你能把印数据也发出去吗?因为很难弄清楚你的代码应该做什么。非常不清楚,但至少你应该取消行
infielist.append(str(totalsale_each))
。目前它在范围内计数(5)循环中,这解释了为什么每个销售人员都有几行。销售人员应该做什么?上面编辑的那一行如何?@Junuxx这是我从取消记录infielist.append(str(totalsale\u each))得到的结果A B 70.0 C D E 70.0
感谢您的评论和建议,但如果我使用break,它将不会继续内部循环。我想做的是,当销售>300时,我想找另一个人(例如主管),因为内部的计数范围(5)
是客户的销售项目,因此其他人需要完成该客户剩余项目的服务。所以我需要它来继续循环。所以你希望循环继续,但计入主管的总数?是的。有什么解决办法吗?感谢您提供的代码,为什么销售人员的最后一次销售(即>300)没有添加到第一个销售人员?e、 g.A,#1:100,#2:200,#3:500,总计:800;B、 #4:200,总计:200
但当我运行代码时:它显示:例如,A,#1:100,#2:200,#3:500,总计:300;B#4:200,总计:700
这很正常,我以为这就是你想要的,我会更新并解释。
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
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