1 / 21

COMPSCI 101 S1 2014 Principles of Programming

COMPSCI 101 S1 2014 Principles of Programming. 27 Programs that interact with the user. Recap: Conditional Iteration. Conditional Iteration: The while Loop The while loop can be used to describe conditional iteration

margot
Download Presentation

COMPSCI 101 S1 2014 Principles of Programming

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. COMPSCI 101 S1 2014Principles of Programming 27 Programs that interact with the user

  2. Recap: Conditional Iteration • Conditional Iteration: The while Loop • The while loop can be used to describe conditional iteration • Conditional iteration requires a condition to be tested within the loop to determine if it should continue • Condition is tested at top of loop • Statements within loop can execute zero or more times • Improper use may lead to infinite loop while <condition>: <sequence of Statements> COMPSCI101

  3. Recap: Input, Processing, and Output • Programs usually accept inputs from a source, process them, and output results to a destination • In terminal-based interactive programs, these are the keyboard and terminal display, respectively • Reading input from the Console: • Using the input function • Print a prompt, wait for the user input and return a String Enter a string: Peter name = input("Enter a string: ") Peter print (name) COMPSCI101

  4. contacts = {'bill': '353-1234', 'rich': '269-1234', 'jane': '352-1234'} Recap: Dictionaries • A dictionary organizes information by association, not position • It is written as a sequence of key/value pairs separated by commas • Pairs are sometimes called entries • Enclosed in curly braces ({ and }) • A colon (:) separates a key and its value • { key1:value1, key2:value2, … } • Keys must be immutable values such as strings, numbers and tuples • Values can be of any Python data type • Unordered collection of references • Adding Keys and Replacing Values • Add a new key/value pair to a dictionary using [ ] • Use [ ] also to replace a value at an existing key contacts['peter']='123-4567' COMPSCI101

  5. Recap: Operators & Methods • Common Operators: • len(my_dict)– number of key:value pairs in the dictionary • element in my_dict – boolean, is element a keyin the dictionary? • for key in my_dict– iterates through the keys of a dictionary • Dictionary Methods • my_dict.items() – all the key/value pairs • my_dict.keys() – all the keys • my_dict.values() – all the values • Examples: for key in my_dict: print (key) for k in my_dict: print (k) for key,value in my_dict.items(): print (key,value) for value in my_dict.values(): print (value) COMPSCI101

  6. Learning outcomes • At the end of this lecture, students should be able to: • Write programs that interact with the user • Examples and Exercises: • Case study 1: Printing the frequency of each word in the text. • Case study 2: Phonebook Lookup Application • Case study 3: Building a Phonebook Application • References: • Dictionaries • https://docs.python.org/3.3/tutorial/datastructures.html#dictionaries COMPSCI101

  7. Case Study 1Printing Frequency Table • Task: • Complete the word_count()function which takes a piece of text as input and prints the frequency of each word in the text. You may assume that the text has no punctuation and words are separated by blank spaces. • Arguments: text (a string) • Prints: the frequency table • Example: others appears 1 time. than appears 1 time. some appears 1 time. equal appears 2 times. but appears 1 time. are appears 2 times. animals appears 2 times. all appears 1 time. more appears 1 time. >>> word_count('all animals are equal but some animals are more equal than others') Unordered collection COMPSCI101

  8. Case Study 1Printing Frequency Table • Steps: How? word_count('all animals are equal but some animals are more equal than others') Examples: … COMPSCI101

  9. Case Study 1Creating Entries word_count('all animals are equal but some animals ... • Iterate through all items in the word list equals all are but animals … 1 1 1 1 2 1 some COMPSCI101 1

  10. Case Study 1Printing Frequency Table Demo 1 • Steps: • How to check for existence? • How do we increment the value of the corresponding entry? • How do we create a new entry? if word in counters: counters[word] = counters[word] + 1 counters[word] = 1 COMPSCI101

  11. Exercise 1: Printing Frequency Table • Task: • Complete the word_count2()function which asks the user to enter a message and prints the frequency of each word in the message. • Prints: the frequency table • Example: >>> word_count2() Enter a message: all animals are equal but some animals are more equal than others others appears 1 time. than appears 1 time. some appears 1 time. equal appears 2 times. but appears 1 time. are appears 2 times. animals appears 2 times. all appears 1 time. more appears 1 time. COMPSCI101

  12. Case Study 2Phonebook Lookup Application • Task: • Complete the lookup() function which takes a phonebook dictionary as input. In the dictionary, the names of individuals (the keys) are mapped to strings containing phone numbers (the values). Your function should provide a simple user interface through which a user can enter the name of an individual and obtain the phone number assigned. • Arguments: phonebook dictionary • Prints: phone number of an individual • Example: The dictionary phonebook >>> lookup({"Angela":"3737599-85654", "Ann":"3737599-84947", "Patricia":"3737599-87093","Andrew":"3737599-85654"}) COMPSCI101

  13. Case Study 2Phonebook Lookup Application • Input – Process – Output • Input: the phonebook dictionary • Process – lookup • Output – return the phone number >>> lookup({"Angela":"3737599-85654", "Ann":"3737599-84947", "Patricia":"3737599-87093","Andrew":"3737599-85654"}) Enter the name: Angela 3737599-85654 Do you want to continue(y/n)? y Enter the name: Ann 3737599-84947 Do you want to continue(y/n)? y Enter the name: abc The name you entered is not known Do you want to continue(y/n)? n COMPSCI101

  14. Case Study 2Input: Dictionary • The phonebook dictionary: • Existence check: • element in my_dict – returns a boolean. Is elementa key in the dictionary? • If the result is yes, prints the value (phone number) of the corresponding entry COMPSCI101

  15. Case Study 2Algorithm n Continue? y/n y Enter a name Does the dictionary contain the name? Print an error message True Print the phone number COMPSCI101

  16. Case Study 2Algorithm Demo 2 COMPSCI101

  17. Case Study 3Create a phonebook dictionary • Task: • Complete the create_phonebook()function which takes a filename as input and builds a phonebook dictionary. You may assume that the text has no punctuation and keys and values are separated by blank spaces. • Arguments: a filename • Prints: a phonebook dictionary • Example: Angela 3737599-85654 Ann 3737599-84947 Patricia 3737599-87093 Andrew 3737599-85654 phonebook.txt my_phonebook = create_phonebook("phonebook.txt") {"Angela":"3737599-85654", "Ann":"3737599-84947", "Patricia":"3737599-87093","Andrew":"3737599-85654"} COMPSCI101

  18. Angela 3737599-85654 Ann 3737599-84947 Patricia 3737599-87093 Case Study 3Algorithm Demo 3 COMPSCI101

  19. Advanced ExampleShowcase: A phonebook Application Demo 4 • A terminal-based interactive program for the phonebook. • A menu is printed and ask you whether you want to print out all phone numbers, add a phone number, remove a phone number, lookup a phone number, load numbers from a text file, or save numbers to a text file • The same menu is printed every time you finish adding, saving, etc. And the program should still run. • The application stops when the user enters a number 7. 1. Print Phone Numbers 2. Add a Phone Number 3. Remove a Phone Number 4. Lookup a Phone Number 5. Load numbers 6. Save numbers 7. Quit Type in a number (1-7): COMPSCI101

  20. Sample Output 1. Print Phone Numbers 2. Add a Phone Number 3. Remove a Phone Number 4. Lookup a Phone Number 5. Load numbers 6. Save numbers 7. Quit Type in a number (1-7): 2 Add Name and Number Name: Jill Number: 1234 Type in a number (1-7): 2 Add Name and Number Name: Fred Number: 4321 Type in a number (1-7): 1 Telephone Numbers: Name: Jill Number: 1234 Name: Fred Number: 4321 Type in a number (1-7): 7 Goodbye COMPSCI101

  21. Summary • A dictionary associates a set of keys with values • Curley braces ({}) are used to create a dictionary • []is used to add a new key/value pair to a dictionary or to replace a value associated with an existing key • Keys must be immutable values such as strings, numbers and tuples • It is an unordered collection of references We will learn tuples next week! COMPSCI101

More Related