How do I write the file without repeating in the loop?
How to continue writing the loop to the file and how to reset the initial value to 0 after each condition met?
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()
This is my python test:
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
The file I am getting after running is
A
100.0
250.0
B
250.0
450.0
C
200.0
D
200.0
E
200.0
400.0
but what I want to get is like below:
A
600
B
200
C
700
D
500
E
200
How can I correct it?
I can’t figure out where to put infilelist.append(sales_person) and infilelist.append(totalsale_each) after asking for the next sales person when sales exceed 300.
Thank you
If I understand correctly, when the sales go over 300, you want to write the total sales of that person, and ask for the next one.
So, what you need to do is, add the total of sales for each person, after calculating the total sales, which means after the loop in
range(5).Also, you need to stop calculating sales after you find that a sales person has sold over 300. So, you need to
breakthe loop at that moment, and continue normally.The problem, here, is to go to the next sales person after that. Either you continue the loop and it will prompt as it would for any other name, OR you should ask for it in another form, and insert it in the array, and make sure it doesn’t ask for another name like it would normally. The first option is simpler, if you need the second say so in the comment.
As a result, I would suggest these modifications to the
salesfunction:EDIT: I edited the code: removed the
break, write the last one’s total, ask for another sales person, reset the total counter, and write everything in his name.Also, a note about the return statement (maybe you already know that): it will return the last sales person’s total sales and name.
However, I would suggest to rewrite the whole thing, so that you save the totals in a dictionary, with a key = the sales person’s name or index (sales_no). After that, you write them all to the list.
EDIT 2: If you notice, after adding the new sales_person (
infilelist.append(sales_person)), I am adding the sales tototalsales_each. It seems what you want is the opposite, so you should do this before adding the new sales person, which gives you: