


Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!



CS112 MidTem Exam Solutions Part 1:
Assumptions: -‐ Main menu option choices by user will be limited to the integers 1, 2 or 3. -‐ Nested (sub) menu option choices by user will be limited to the integers 1 or 2. -‐ Professor’s names are known (i.e., not entered via standard input) -‐ Error handling for data types other than integers is not required. Data: -‐ main menu text (string) -‐ professor names (list -‐ immutable) -‐ messages (list of lists of strings – mutable) Operation: -‐ initialize: o main menu text (Options: Type New Message, View Messages, Exit) o main loop sentinel value o immutable list of professor names o mutable list of message strings belonging to each professor -‐ do while user chooses not to terminate application (Exit option) o display menu & get option from user o if option == “Type New Message” § display professor’s names and get user selection § get message § add message to professor’s list of messages o if option == “Display Messages” § display professor’s names and get user selection § display messages for selected professor o if option == “Exit” § display exit message o else § display error message
def main(): menu = "\nMessage Machine\n-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐\n" +
" 1) Type New Message\n" +
" 2) View Messages\n" +
" 3) Exit\n" option = 0 professor = ("Prof. Hofbrau","Prof. Ali") message = [[],[]] while option != 3: print menu option = input("Enter Option => ") if option == 1: print "Leave a message for:\n\n" +
" 1) " + professor[0] + "\n" +
" 2) " + professor[1] + "\n" option = int(raw_input("==> ")) text = raw_input("\nEnter Message => ") message[option-‐1].append(text) elif option == 2: print "\nView messages for:\n\n" +
" 1) Prof. Hofbrau\n" +
" 2) Prof. Ali\n" option = int(raw_input("==> ")) print "\nMessages for: " +
professor[option-‐1] + "\n" for each_message in message[option-‐1]: print each_message elif option == 3: print "\nGoodbye!\n" else: print "\nInvalid Option!\n" main()
def main(): menu = "\nVoting Machine\n-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐\n" +
" 1) Vote\n" +
" 2) View Current Results\n" +
" 3) Exit\n" option = 0 candidate = ("George Patriot","Cassandra Mason") vote_tally = [0,0] while option != 3: print menu option = input("Enter Option => ") if option == 1: print "Vote for:\n\n" +
" 1) " + candidate[0] + "\n" +
" 2) " + candidate[1] + "\n" vote = input("Enter Vote => ") vote_tally[vote-‐1] = vote_tally[vote-‐1] + 1 elif option == 2: print "\nView Current Results:\n" for i in range(len(vote_tally)): print candidate[i] +
" => " + str(vote_tally[i]) elif option == 3: print "\nGoodbye!\n" else: print "\nInvalid Option!\n" main() (NOTE) – There numerous ways to write a pseudocode algorithm that provides a solution to a particular programming problem. There are also many ways to implement a program specification. The samples provided represent one perspective.