Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7957921
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T04:20:20+00:00 2026-06-04T04:20:20+00:00

How do I write the file without repeating in the loop? How to continue

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T04:20:21+00:00Added an answer on June 4, 2026 at 4:20 am

    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 break the 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 sales function:

    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
    

    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 to totalsales_each. It seems what you want is the opposite, so you should do this before adding the new sales person, which gives you:

    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
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a file which I write some java objects. After writing, sometimes I
I want to write to a file without overwriting anything. It is a text
Is there any workaround to allow actionscript3 write to file system without adobe air?
I'm writing a MIDlet which needs to write file. I'm using FileConnection from JSR-75
how can i write data to file without erasing the old content
I have been trying to write to a file,but it kept writing them on
In Cocoa, how can I write a string to a text file without replacing
If you work with FileOutputStream methods, each time you write your file through this
I want to write data to a .txt file without replacing its older contents.I
Whats the best way to read/write file properties (like author, description, etc) in C#?

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.