asweigart/pyinputplus

Bug Found in the inputMenu Method

Closed this issue · 1 comments

When I was using the inputMenu Method, I found that if I pass a prompt argument into the method, the menu won't be printed out, so I have to leave the prompt message as the default if I want the menu shown.

I've checked the source code, and it seems to be just a small indentation problem. I tried to fix it but gave up at the end because I didn't figure out how to test it.

The code is at line 770 - 782 in the src file. I think unindentating the sections after the first if statement should be sufficient, but I'm not sure if any else statement will be needed in the first if statement.

    if prompt == "_default":
        prompt = "Please select one of the following:\n"
    if numbered:
        prompt += "\n".join(
            [str(i + 1) + ". " + choices[i] for i in range(len(choices))]
        )
    elif lettered:
        prompt += "\n".join(
            [chr(65 + i) + ". " + choices[i] for i in range(len(choices))]
        )
    else:
        prompt += "\n".join(["* " + choice for choice in choices])
    prompt += "\n"

Also, many thanks for your great work with the book Automate the Boring Stuff with Python! It really got me in love with programming and saved me a lot of time at work! @asweigart

This is intentional, so that if someone wants to print the menu in their own custom style, they can do so with the prompt argument. However, you're the second person to bring up this issue, I'm thinking lots of people are making this "mistake" and so the real mistake is with the way I designed the code.

I'll update it to your design, so that specifying a prompt argument only replaces the "Please select one of the following:" text and doesn't erase the menu text. Thanks!