Question
Using python 3.6 How do I incorporate 0 or negative input to raise an error and let the user try again? like len(header): print(Invalid choice!!
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()
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started