I have a script that I wrote in python for testing out the sorting algorithms that I've implemented. The main part of the program asks the user to select on of the sort algorithms from a list. And then whether they would like to sort from a file of numbers or select a list of random numbers. I have it setup (i think) so that typing a number thats not in the first list of choices will just print "Bad choice" and try getting the number again.
[Edit] After taking the advice of the answer I changed a couple of the inputs to raw inputs. And i changed the structure of the program. It now works perfectly except it still prints out "Bad Choice" even after a success.
def fileOrRandom():
return raw_input("Would you like to read from file or random list? (A or B): ")
choices = {1:SelectionSorter,2:ComparisonSorter}
print "Please enter the type of sort you would like to perform."
print "1. Selection Sort\n2. Comparison Sort"
while(True):
try:
choice=input("Your choice: ")
for i in range(2):
if(choice==i+1):
choice2=fileOrRandom()
if(choice2=='A'):
fileName=raw_input("Please Enter The Name of the File to sort: ")
sorter = choices[choice](fileName)
sorter.timedSort(sorter.sort)
elif(choice2=='B'):
num = input("How many random numbers would you like to sort: ")
sorter = choices[choice](None,num)
sorter.timedSort(sorter.sort)
else:
raise Exception
else:
raise Exception
break
except Exception:
print "Bad Choice"
My issue is that it works as expected for the first part where it will return bad choice for a number thats not in the list, and it will get fileOrRandom(), but it still prints "Bad Choice" when selecting good values should print out my results because sorter.timedSort(sorter.sort)
executes my sorting algorithm and prints out a bunch of other things to the screen. Am I just missing something simple or is there a better way to deal with these nested options in a python program?