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

Using Python 3.6 How do I add except ValueError into the loop so that if user ty

ID: 3860439 • Letter: U

Question

Using Python 3.6

How do I add except ValueError into the loop so that if user types -->dasfljadsfl--> instead of a valid number, 1 2,3

Enter the name of the file you would like to process: countries.csv
1. Area
2. GDP
3. Population
Enter choice: adfs
An error occurred
>>>

like output

Enter the name of the file you would like to process: countries.csv
1. Area
2. GDP
3. Population
Enter choice: adfs

Invalid input try again, enter choice:ae,,,...e

invalid input try again enter choice: 3

population is the seconde most interesting thing to learn about 8 billion people

would you like to chose another file (y/n)? n

hear is the loop of concern

#loop to check valid input option.

                                while True:

                                        if choice>len(header):

                                                print("Invalid choice!! Please try again and select value either:",end=" ")

                                                for i in range(1,len(header)+1):

                                                        print(i,end=",")

                                        else:

                                                break

                                        choice = int(input(' Enter choice: '))

here is the full code

import os.path

import sys, traceback

def main():

        print('Welcome to CSV Analytics!')

        try:

                fname = None

                # running an infinite loop and will break only will another attribute and another file choice selected as 'n'

                while True:

                        # will take file name input only when fname is not None

                        if fname is None:

                                fname = input('Enter the name of the file you would like to process: ')

                                if not validateFile(fname):

                                        fname = None

                                        continue

                                       

                        # obtaing header from the file

                        with open(fname) as fp:

                                header = fp.readline().strip().split(',')

                                # first column is object name, so skipping that and getting rest of the columns                        

                                header = header[1:]

                                seq = 1

                                # printing the options menu which are header in the file

                                for attribs in header:

                                        print(str(seq)+'.', attribs)

                                        seq += 1

                                # taking user choice

                                choice = int(input('Enter choice: '))

                                #loop to check valid input option.

                                while True:

                                        if choice>len(header):

                                                print("Invalid choice!! Please try again and select value either:",end=" ")

                                                for i in range(1,len(header)+1):

                                                        print(i,end=",")

                                        else:

                                                break

                                        choice = int(input(' Enter choice: '))

                             

                                               

                                usr_choice = header[choice - 1]

                                max_val = None

                                max_obj = None

                                # finding maximum value for the given attribute selection

                                for line in fp:

                                        attr_list = line.strip().split(',')

                                        # when max_val is none set the max_val and max_obj to attribute of first line

                                        if max_val is None:

                                                max_val = attr_list[choice]

                                                max_obj = attr_list[0]

                                        # verifying maximum value of given attribute choice here

                                        if attr_list[choice] > max_val:

                                                max_val = attr_list[choice]

                                                max_obj = attr_list[0]

                                print('Largest', usr_choice, 'value is', max_obj, 'with', max_val)

                                another_attr = input('Would you like to conduct another analysis on an attribute? (y/n) ')

                                if another_attr == 'y':

                                        continue

                                else:

                                        while True:

                                                another_file = input('Would you like to evaluate another file? (y/n) ')

                                                if another_file == 'y':

                                                      fname = input('Enter the name of the file you would like to process: ')

                                                        if validateFile(fname):

                                                                break

                                                        else:

                                                                continue

                                                else:

                                                        return

        # catches all file I/O errors

        except IOError:

                print('Errored while doing I/O operations on the file')

                #traceback.print_exc(file=sys.stdout)

        # all non file I/O errors captured here

      except:

                print('An error occurred')

                #traceback.print_exc(file=sys.stdout)

def validateFile(fname):

        if os.path.exists(fname):

                return True

        else:

                print('File not found, try with correct file name')

                return False

if __name__=='__main__':

        main()

Explanation / Answer

import os.path

import sys, traceback

def main():

        print('Welcome to CSV Analytics!')

        try:

                fname = None

                # running an infinite loop and will break only will another attribute and another file choice selected as 'n'

                while True:

                        # will take file name input only when fname is not None

                        if fname is None:

                                fname = input('Enter the name of the file you would like to process: ')

                                if not validateFile(fname):

                                        fname = None

                                        continue

                                       

                        # obtaing header from the file

                        with open(fname) as fp:

                                header = fp.readline().strip().split(',')

                                # first column is object name, so skipping that and getting rest of the columns                        

                                header = header[1:]

                                seq = 1

                                # printing the options menu which are header in the file

                                for attribs in header:

                                        print(str(seq)+'.', attribs)

                                        seq += 1

                                # taking user choice

                                choice = int(input('Enter choice: '))

                                #loop to check valid input option.

                                while True:

                                        if choice>len(header):

                                                print("Invalid choice!! Please try again and select value either:",end=" ")

                                                for i in range(1,len(header)+1):

                                                        print(i,end=",")

                                        else:

                                                break

                                        choice = int(input(' Enter choice: '))

                             

                                               

                                usr_choice = header[choice - 1]

                                max_val = None

                                max_obj = None

                                # finding maximum value for the given attribute selection

                                for line in fp:

                                        attr_list = line.strip().split(',')

                                        # when max_val is none set the max_val and max_obj to attribute of first line

                                        if max_val is None:

                                                max_val = attr_list[choice]

                                                max_obj = attr_list[0]

                                        # verifying maximum value of given attribute choice here

                                        if attr_list[choice] > max_val:

                                                max_val = attr_list[choice]

                                                max_obj = attr_list[0]

                                print('Largest', usr_choice, 'value is', max_obj, 'with', max_val)

                                another_attr = input('Would you like to conduct another analysis on an attribute? (y/n) ')

                                if another_attr == 'y':

                                        continue

                                else:

                                        while True:

                                                another_file = input('Would you like to evaluate another file? (y/n) ')

                                                if another_file == 'y':

                                                      fname = input('Enter the name of the file you would like to process: ')

                                                        if validateFile(fname):

                                                                break

                                                        else:

                                                                continue

                                                else:

                                                        return

        # catches all file I/O errors

        except IOError:

                print('Errored while doing I/O operations on the file')

                #traceback.print_exc(file=sys.stdout)

        # all non file I/O errors captured here

      except:

                print('An error occurred')

                #traceback.print_exc(file=sys.stdout)

def validateFile(fname):

        if os.path.exists(fname):

                return True

        else:

                print('File not found, try with correct file name')

                return False

if __name__=='__main__':

        main()

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote