Write a program to store water usage data of 4 customers in a text file. The program asks the user to enter account number, customer type (R for residential or B for business), and number of gallons used for each of the 4 customers. Store the data in a text file named "water.txt". Overwrite old data in "water.txt" if the file already exists.

Respuesta :

Answer:

with open('water.txt','w') as file:

   i = 4

   while i > 0:

       accNo = input('Enter Account Number: ')

       bType = input('Enter Customer Type R or B: ')

       nGallons = input('Enter Number of gallons: ')

       ls = ' '.join(['Account Number:', accNo,'\nType:', bType, '\nNumber Of gallons:',nGallons,'\n\n'])

       file.write(ls)

       i -= 1

   

Explanation:

The programming language used is python.

A file water.txt is created, and a counter is initialized to ensure data is collected for all the the customers.

A loop is created to collect the input of the four customers. The data is collected, assigned to a list and joined using the join method, to create a string of the data collected and it is written to the file.

Using a WITH statement allows a file to automatically close.

Otras preguntas