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 incorporate 0 or negative input to raise an error and

ID: 3860454 • Letter: U

Question

Using python 3.6

How do I incorporate 0 or negative input to raise an error and let the user try again?

like <=0

#loop to check valid input option.

while True:
try:
choice = int(input('Enter choice: '))
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: '))
except ValueError as ve:print('Invalid response, You need to select the number choice from the option menu') # Catch your local ValueError and continue your loop
  
  
  
usr_choice = header[choice - 1]

max_val = None
max_obj = None

--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

                               

                               #loop to check valid input option.

                                while True:

                                        try:

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

                                                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: '))

                                        except ValueError as ve:print('Invalid response, You need to select the number choice from the option menu') # Catch your local ValueError and continue your loop

       

                                               

                                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

You just need to modify the following code, Let me know if you have any doubt. You need to check the input value whether it is 0 or negative in the if condition itself.

#loop to check valid input option.

while True:

if choice>len(header) or choice<=0:

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: '))

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