开发者

Nested Choices in a python program [closed]

开发者 https://www.devze.com 2022-12-18 09:14 出处:网络
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.

This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.

Closed 5 months ago.

Improve this question

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 one 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 set up (I think) so that typing a number that's 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 that's not in the list, and it will get fileOrRandom(), but it still prints "Bad Choice" when selecting good values that 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?


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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号