75.1k views
0 votes
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.

1 Answer

2 votes

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,'\\Type:', bType, '\\Number Of gallons:',nGallons,'\\\\'])

file.write(ls)

i -= 1

Step-by-step 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.

User Stephane Bersier
by
7.8k points

No related questions found