Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 570347
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T13:23:30+00:00 2026-05-13T13:23:30+00:00

I have a script that I wrote in python for testing out the sorting

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T13:23:30+00:00Added an answer on May 13, 2026 at 1:23 pm

    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
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 310k
  • Answers 310k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer After 15 hours of struggle (half of which was devoted… May 13, 2026 at 10:13 pm
  • Editorial Team
    Editorial Team added an answer I'm assuming that xml is the XDocument object itself. An… May 13, 2026 at 10:13 pm
  • Editorial Team
    Editorial Team added an answer The pattern outlined by the MSDN is the only correct… May 13, 2026 at 10:13 pm

Related Questions

Greetings. I have written a little python script that calls MySQL in a subprocess.
I have a JavaScript slideshow that pre-loads images out of a MySQL database and
I have a python script that is a http-server: http://paste2.org/p/89701 , when benchmarking it
If I have a Python script that requires at least a particular version of
I understand that letting any anonymous user upload any sort of file in general

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.