0

In my Python script I have a menu which accepts user input, then based on that input will print some lines with information. It looks kind of like this:

while True:
    # Print the menu
    choice = raw_input('Pick an option: ')
    if choice == 'item_1':
        # Display item_1's contents
    elif choice == 'item_2':
        # Display item_2's contents
    # etc...

The information displayed by each item_ is slightly larger than I want it to be, so I want to split each of them into multiple pages.

To access those different pages I want it to be based on the user input, but rather than having an entirely different input for each page, I want the user to be able to write the page number next to the original input (with a space in between). So if the user inputs item_1 or item_1 1 they get the first page of item_1, and if they input item_2 2 they get the second page of item_2, and so on.

How can I break the user input into parts to do the aforementioned?

===========================================================================

EDIT

I was a little bit misleading about each item and what it does. Earlier in the script I have this:

def item_1():
    # Print line 1
    # Sleep
    # Print line 2
    # Sleep
    # Print line 3

def item_2():
    # Print line 1
    # Sleep
    # Print line 2
    # Sleep
    # Print line 3

What I really want to do is define more functions, one for each page, and then call them when the user inputs a number after the `item' (sort of like parsing arguments). Like this:

Input item_1 or item_1 1 >> Output item_1()

Input item_1 2 >> Output item_1_p2()

Input item_3 4 >> Output item_3_p4()

And, as seen in the first input example, I'd like the first page of a given input to be displayed if no pages are mentioned in the input.

4
  • choice.split() ... Commented Dec 31, 2015 at 6:20
  • Your plan of making a function for each item is very bad. You would have to add code to the program every time you added new items to the database or content file. As an aside, I don't see you mention your excellent reason for calling sleep() after every line, so I assume you don't have one. Don't do it, or your users will quickly become frustrated. Commented Dec 31, 2015 at 7:59
  • Well, the functions are sort of "animated" with a sleep time of 0.1 seconds, it's kind of how I want to keep it. Again, sorry for the earlier misinformation, but I'd like to add that all of the functions in this script are actually stored in another file, and I import them to this one, so adding or modifying isn't really an issue. The only issue I do have is having to write out elif choice == 'item_1 2': item_1_p2() and so on for each page in my main script. Commented Dec 31, 2015 at 8:11
  • You say that it "isn't really an issue" and then immediately describe the issue it directly causes? The difficulties you're having are a demonstration of the difficulties you will continue to have if you insist on keeping your planned program structure (and that delay will annoy your users, unless you're dong it to give them time to see output that extends past more than one screen, in which case your program should show one screen at a time and advance it on user input, like most common terminal programs do). Commented Dec 31, 2015 at 8:27

1 Answer 1

2

Use a dictionary that holds a list of strings for each item:

book = {'item_1':['page 1', 'page 2', 'page 3'], 'item_2':['page 1', 'page 2', 'page 3']}

while True:
    choice = raw_input('Pick an option: ').split()
    if not choice:
        break
    if len(choice) == 1:
        for page in book[choice[0]]:
            print page
    else:
        print book[choice[0]][int(choice[1])-1]

First it splits the input on whitespace. If the user had simply hit Enter with no characters, it'll stop. Otherwise, it'll do the following:

  1. Check how many terms resulted from the split
  2. If there's only one term, it's an 'item_x', so print the whole entry
  3. If there's more than one term, it's 'item_x' and a page number, so print the proper page
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks for the suggestion. I played around a little bit with your code above but couldn't get it to do exactly what I want. Still, useful for those instances when I want to print strings instead of calling functions.
As I've said, you shouldn't be creating a mountain of functions for this in the first place.
But that's how I want the program to work. All I'm looking for is a solution to the dilemma I'm facing (that is, splitting user input and interpreting each part in its own way), without changing the overall structure of how the script works. And the time.sleep(0.1) in each function isn't about giving users time to see things, but rather simulating an animation, and I don't see why it's a bad thing. Nobody complained about it, in fact users liked it. Sorry if that came off as aggressive, I just insist on only changing things that I want to be changed, not the entire program structure.
Boy was I naive

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.