views:

82

answers:

1

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?

A: 

use raw_input()

def fileOrRandom():
  return raw_input("Would you like to read from file or random list? (A or B): ")

your while loop should look like this (after fixing the indentation)

while True :
    choice=raw_input("Your choice: ")
    for i in range(2):
     if choice==i+1 and fileOrRandom()=="A" :
       fileName=raw_input("Please Enter The Name of the File to sort: ")
       sorter = choices[choice](fileName)
       sorter.timedSort(sorter.sort)
     elif choice==i+1 and fileOrRandom()=="B" :
       num = raw_input("How many random numbers would you like to sort: ")
       sorter = choices[choice](None,num)
       sorter.timedSort(sorter.sort)
     elif choice in ['q','Q']: break
     else: print "Bad choice"
ghostdog74
the break statement is indented one too many in your answer because it doesn't work at all with it that way in mine. With the break in the right place it still prints "Bad Choice" after it executes with option B
controlfreak123
use raw_input() for all your input() and try again. also , remove all the braces in your if statements as they are redundant. lastly, i propose to break out of the loop if user enters q or Q.
ghostdog74
since you are using infinity loop, i see no reason using the try/exception.
ghostdog74
Actually, the `input` s which are supposed to return numbers should not be replaced with `raw_inputs` (or else the OP would have to check for `"1"` rather than `1`).
Michał Marczyk
I actually solved my problem by catching the errors that are raised when (you type a number instead of a letter) for example. that kept it from always raising an exception
controlfreak123