Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Creating a Menu
#1
Hello everyone. I am in my 3rd week of my first programming class and I am struggling. The first two weeks were no problem. Currently, I am working on an assignment with multiple steps. It's a long one but I am only on step 2 and I'm completely lost on where to begin with creating a menu in the way they want. I have looked up YouTube videos but they show how to use a function like menu() instead using function with string parameters like my assignment is asking.


Here is step 2 of the assignment:

(2) Implement a print_menu() function, which has a string as a parameter, outputs a menu of user options for analyzing/editing the string, and returns the user's entered menu option and the sample text string (which can be edited inside the print_menu() function). Each option is represented by a single character.

If an invalid character is entered, continue to prompt for a valid choice. Hint: Implement the Quit menu option before implementing other options. Call print_menu() in the main section of your code. Continue to call print_menu() until the user enters q to Quit. (3 pts)

Ex:

MENU
c - Number of non-whitespace characters
w - Number of words
f - Fix capitalization
r - Replace punctuation
s - Shorten spaces
q - Quit

Choose an option:


This is my code so far:
def print_menu(usr_str):
    menu_op = ' '
    menu_op += 'MENU'
    menu_op += 'c - Number of non-whitespace characters'    
    menu_op += 'w - Number of words'
    menu_op += 'f - Fix capitalization'
    menu_op += 'r - Replace punctuation'
    menu_op += 's - Shorten spaces'
    menu_op += 'q - Quit' 
    return menu_op, usr_str
   


if __name__ == '__main__':
    # Complete main section of code
    usr_str = str(input('Enter a sample text:''\n'))
    print('\nYou entered:',usr_str)
    print(print_menu(usr_str))
Here is the output: (keep in mind I've already done step 1 and that's why there is more than just the menu)

Enter a sample text:

You entered: we'll continue our quest in space. there will be more shuttle flights and more shuttle crews and, yes; more volunteers, more civilians, more teachers in space. nothing ends here; our hopes and our journeys continue!
(' MENUc - Number of non-whitespace charactersw - Number of wordsf - Fix capitalizationr - Replace punctuations - Shorten spacesq - Quit', "we'll continue our quest in space. there will be more shuttle flights and more shuttle crews and, yes; more volunteers, more civilians, more teachers in space. nothing ends here; our hopes and our journeys continue! ")



Problem:

I guess I don't know how to create a menu as a string. When I try looking up how to add to an empty string.. it suggests:
s = s + 'Hello'
but this is not working in the case of a menu. As you can see, I haven't been able to move on to the part of implementing what each selection of the menu will do yet. Sigh. I really want to learn this the right way so, any help or guidance is greatly appreciated.
Reply
#2
Read the assignment carefully. You don't have to do the menu as a single string. You can use multiple print statements to hand that, one for each line. The user's choice is what the function is supposed to return, not the whole string. So you will need to use the input() function to get that choice from the user. Then you will need a if/elif/else chain to handle the different options.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
Thanks! I realized that after I posted! I had to step away for a bit and then I was working on some other assignments. This is where I am at so far...

def print_menu(usr_str):
    menu_op = ' '
    print()
    print('MENU')
    print('c - Number of non-whitespace characters')
    print('w - Number of words')
    print('f - Fix capitalization')
    print('r - Replace punctuation')
    print('s - Shorten spaces')
    print('q - Quit')
    print()      
    print()
    if choice == 'c':
        for ch in usr_str:
            usr_str.replace(' ','')
            print(usr_str)
    elif choice == 'w':
        res = len(usr_str.split())
        print(res)    
    return menu_op, usr_str
   


if __name__ == '__main__':
    usr_str = str(input('Enter a sample text:''\n'))
    print('\nYou entered:',usr_str)
    choice = input('Choose an option:')
    print(print_menu(usr_str))
Reply
#4
Two comments - the name of the function no longer reflects its use. print_menu should print the menu, handling the choice should be handled elsewhere or else change the function name.

Second, choice is referenced inside print_menu but is not in that namespace. If you pass choice in to your function as well as use_str it should work better.
Reply
#5
You ask for a choice before you show the menu. How are they supposed to know what to choose? You need to show the menu, get the choice, and then process the choice (do what they chose).

You don't need the loop on line 14. The replace on line 15 takes care of all the spaces, you just need to run it one. But that replace doesn't change usr_str, it creates a new string without the spaces. You need to assign that to a new variable name, and then print the len() of that new variable.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020