Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

debugging python read sales.py def main(): # Open the sales.txt file for reading

ID: 3862853 • Letter: D

Question

debugging python

read sales.py

def main():

# Open the sales.txt file for reading.

sales_file = open('sales.txt', 'r')

# From program 6-12

# Initialize an accumulator to 0.0

total = 0.0

# From program 6-12

# Initialize a variable to keep count of the sales.

count = 0

#From program 6-12

print('Here are the sales for each day entered')

# Read all the lines from the file.

# Get the values from the file and total them.

for line in sales_file

# Convert a line to a float.

amount = float(line)

# Add 1 to the count variable.

count += 1

# Display the sales.

print('Day #', count, ': ', format(amount, '.2f'), sep='')

# Add the time to total.

total += count

# Close the file

sales_file.close()

#From program 6-12

# Display the total of the running times.

print('The total amount in sales is', format(amount, '.2f'))

# Call the main function

main()

writesales.py

# write_sales.py

# Chapter 6.2

def main():

# Get the number of days

num_days = int(input('For how many days do ' +

'you have sales? ')

# Open a new file named sales.txt

sales_file = open('sales.txt', 'w')

# Get the ammount of sales for each day and write

# it to the file

  

for count in range(1, numd_ays + 1):

# Get the sales for a day.

sales = float(input('Enter the sales for day #' +

str(count) + ': "))

  

# Write the sales amount to the file

sales_file.write(str(sales) + ' ')

# Close the file.

sales_file.close()

print('Data written to sales.txt.')

# Call the main function

main()

Explanation / Answer

Hi

I have fixed the isses and highlighted the code changes below

sales.py

def main():
# Open the sales.txt file for reading.
sales_file = open('sales.txt', 'r')

# From program 6-12
# Initialize an accumulator to 0.0
total = 0.0

# From program 6-12
# Initialize a variable to keep count of the sales.
count = 0

#From program 6-12
print('Here are the sales for each day entered')

# Read all the lines from the file.
# Get the values from the file and total them.
for line in sales_file:
# Convert a line to a float.
amount = float(line)

# Add 1 to the count variable.
count += 1

# Display the sales.
print('Day #', count, ': ', format(amount, '.2f'), sep='')

# Add the time to total.
total += count

# Close the file
sales_file.close()

#From program 6-12
# Display the total of the running times.
print('The total amount in sales is', format(amount, '.2f'))

# Call the main function
main()
Output:

sh-4.3$ python3 main.py                                                                                                                                                                                                                                                

Here are the sales for each day entered                                                                                                                                                                                                                                

Day #1: 1.00                                                                                                                                                                                                                                                           

Day #2: 2.00                                                                                                                                                                                                                                                           

Day #3: 3.00                                                                                                                                                                                                                                                           

The total amount in sales is 3.00


writesales.py

# write_sales.py


def main():
# Get the number of days
num_days = int(input('For how many days do ' +
'you have sales? '))

# Open a new file named sales.txt
sales_file = open('sales.txt', 'w')

# Get the ammount of sales for each day and write
# it to the file
  
for count in range(1, num_days + 1):
# Get the sales for a day.
sales = float(input('Enter the sales for day #' +
str(count) + ": "))
  
# Write the sales amount to the file
sales_file.write(str(sales) + ' ')

# Close the file.
sales_file.close()
print('Data written to sales.txt.')

# Call the main function
main()

Output:

sh-4.3$ python3 main.py                                                                                                                                                                                                                                                

For how many days do you have sales? 3                                                                                                                                                                                                                                 

Enter the sales for day #1: 1                                                                                                                                                                                                                                          

Enter the sales for day #2: 2                                                                                                                                                                                                                                          

Enter the sales for day #3: 3                                                                                                                                                                                                                                          

Data written to sales.txt.