Jun-25-2019, 12:46 AM
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:
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.
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.