how do we keep prompting the user to enter the right file name when the file name is wrong?   import csv if __name__ == '__main__': try: a = input('Enter name of file:\n') total1 = 0 total2 = 0 result = 0 i = 0 sums = 0 with open(a, 'r+' ) as file : line = file.readline() while line != '' : value = line.rstrip('\n').split(',') total1 = total1 + int(value[0]) total2 = total2 + int(value[1]) i = i +1 line = file.readline() print('File to be processed is: ', '{}'.format(a)) print('Average weight =', '{:.2f}'.format((total1/i), )) print('Average height = ', '{:.2f}'.format((total2/i), )) except IOError: print('File', a, 'not found.')

EBK JAVA PROGRAMMING
9th Edition
ISBN:9781337671385
Author:FARRELL
Publisher:FARRELL
Chapter13: File Input And Output
Section: Chapter Questions
Problem 16RQ
icon
Related questions
Question

how do we keep prompting the user to enter the right file name when the file name is wrong?

 

import csv


if __name__ == '__main__':
try:
a = input('Enter name of file:\n')
total1 = 0
total2 = 0
result = 0
i = 0
sums = 0
with open(a, 'r+' ) as file :
line = file.readline()
while line != '' :
value = line.rstrip('\n').split(',')

total1 = total1 + int(value[0])
total2 = total2 + int(value[1])
i = i +1
line = file.readline()

print('File to be processed is: ', '{}'.format(a))
print('Average weight =', '{:.2f}'.format((total1/i), ))
print('Average height = ', '{:.2f}'.format((total2/i), ))

except IOError:
print('File', a, 'not found.')

 

 

Luau utauit temprate...
if_name_ == '__main_':
found = False
while not found:
try:
a = input('Enter name of file:\n')
found = True
2.
3
4
totali = 0
total2 = 0
result = 0
i = 0
sums = 0
with open(a, 'r+' ) as file :
line = file.readline()
while line != ''
7
8
9
10
11
12
13
14
15
value = line.rstrip('\n').split(',')
16
total1 = totalı + int(value[0])
total2 = total2 + int(value[1])
i = i +1
line = file.readline()
print('File to be processed is: ', '{}'.format(a))
print('Average weight =', '{:.2f}'.format((totali/i), ))
print('Average height = ', '{:.2f}'.format((total2/i), ))|
17
18
19
20
21
22
23
24
except IOEггог:
25
print('File', a, 'not found.')
Transcribed Image Text:Luau utauit temprate... if_name_ == '__main_': found = False while not found: try: a = input('Enter name of file:\n') found = True 2. 3 4 totali = 0 total2 = 0 result = 0 i = 0 sums = 0 with open(a, 'r+' ) as file : line = file.readline() while line != '' 7 8 9 10 11 12 13 14 15 value = line.rstrip('\n').split(',') 16 total1 = totalı + int(value[0]) total2 = total2 + int(value[1]) i = i +1 line = file.readline() print('File to be processed is: ', '{}'.format(a)) print('Average weight =', '{:.2f}'.format((totali/i), )) print('Average height = ', '{:.2f}'.format((total2/i), ))| 17 18 19 20 21 22 23 24 except IOEггог: 25 print('File', a, 'not found.')
Expert Solution
Step 1

The given code keep prompting the user to enter the right file name until the file name is not correct:

import csv
import os
def main():
    a = input('Enter name of file:\n')   # read the file name
    while True:
        if(os.path.exists(a)): # check for file existence 
            total1 = 0
            total2 = 0
            result = 0
            i = 0
            sums = 0
            with open(a, 'r+' ) as file :
                line = file.readline()
                while line != '' :
                    value = line.rstrip('\n').split(',')

                    total1 = total1 + int(value[0])
                    total2 = total2 + int(value[1])
                    i = i +1
                    line = file.readline()
                print('File to be processed is: ', '{}'.format(a))
                print('Average weight =', '{:.2f}'.format((total1/i), ))
                print('Average height = ', '{:.2f}'.format((total2/i), ))    
                return
        else:  # if file is not exists prompt again to read name of file
            print("The file you selected does not exist, please try again")
            a = input("Enter name of file again: ")
            
if __name__ == '__main__':
    main()

steps

Step by step

Solved in 2 steps with 2 images

Blurred answer
Knowledge Booster
File Input and Output Operations
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
EBK JAVA PROGRAMMING
EBK JAVA PROGRAMMING
Computer Science
ISBN:
9781337671385
Author:
FARRELL
Publisher:
CENGAGE LEARNING - CONSIGNMENT
Programming with Microsoft Visual Basic 2017
Programming with Microsoft Visual Basic 2017
Computer Science
ISBN:
9781337102124
Author:
Diane Zak
Publisher:
Cengage Learning
Microsoft Visual C#
Microsoft Visual C#
Computer Science
ISBN:
9781337102100
Author:
Joyce, Farrell.
Publisher:
Cengage Learning,
COMPREHENSIVE MICROSOFT OFFICE 365 EXCE
COMPREHENSIVE MICROSOFT OFFICE 365 EXCE
Computer Science
ISBN:
9780357392676
Author:
FREUND, Steven
Publisher:
CENGAGE L