Download presentation
Presentation is loading. Please wait.
Published byJulia Ray Modified over 9 years ago
2
Problems Rock-Paper-Scissors (fair game) Functions Frenzy Extra Problems Pig Latin Scoring Paper
3
Objective: Play rock-paper-scissors fairly Steps for the program: 1. Choose the computer choice randomly (without revealing this value to the user) 2. Ask the user for their choice (give warning for incorrect choice) 3. Figure out the winner & print out the result
4
Using choice function from random package Example code: from random import * s = choice(['thread','yarn','twine']) print('I chose', s)
5
Execute a block of code repeatedly as long as a condition is satisfied. Application: Keep asking as long as incorrect choice is given by the user Example code: answer = 'no' while answer == 'no': [body of the program] answer = input('Want to stop? ')
6
Problems: 1. Multiplication: mult(n,m) 2. Dot product: dot(L,K) 3. First index: ind(e,L) 4. Scoring Letter*: letterScore(let) 5. Scoring Word: scrabbleScore(S) Recursive Functions: Base case Recursive part
7
Return: product of 2 integers Requirements: Use recursion Use only: addition, subtraction, negation Hints: Use the construction of function power as a guide m * n = m added together n times Handling negative numbers
8
Return: dot product of any 2 lists Return 0.0 when Lists have unequal lengths Empty lists Hints: Use the construction of function mysum as a guide Dot product = sum of products of elements in the same position Example: [5,3] o [6,4] = 5*6 + 3*4 = 42
9
Return: first index of an element in a list Requirements Use Recursion Counting starts at 0 Nonexistence = return any integer ≥ len(L) Examples: >>> ind(42, [55,77,42,12,42,100]) 2 >>> ind('i', 'team') 4
10
Return: score of a letter based on Requirements No recursion needed Return 0 for input that is not from 'a' to 'z' Hints: Use in keyword: >>> 'a' in 'this is a string with letter a' True
11
Return: score of a string Requirements Use recursion Hints: Use the construction of function mylen as a guide Use letterScore(let) as previously defined
12
The warm-up: pigLatin(s) The challenge: spamLatin(s)
13
Input: single word, lower letter string s Translate to Pig Latin based on these rules If s is an empty string: ▪ return an empty string If s begins with a vowel (a,e,i,o,u) ▪ Append 'way' to the end of s ▪ Example: pigLatin('one') returns 'oneway' If s begins with a consonant (the rest including y) ▪ Move this consonant to the end ▪ Then append 'ay' to the end ▪ Example: pigLatin('be') returns 'ebay'
14
Input: single word, lower letter string s Translate to Pig Latin based on these rules If s is an empty string, or begins with a vowel, same with pigLatin(s) If s begins with a consonant ▪ Move all initial consonants to the end ▪ Then append 'ay' to the end ▪ Example: pigLatin('string') returns 'ingstray'
15
Rules (continue) If s starts with 'y' which is followed by ▪ a vowel, then consider this 'y' to be a consonant ▪ a consonant, then consider this 'y' to be a vowel ▪ spamLatin('yttrium') returns 'yttriumway' ▪ spamLatin('yoohoo') returns 'oohooyay‘ Other cases: free to decide
16
Input: a file name Output: Total Scrabble score Total number of alphabetic characters Average Scrabble-score-per-letter Important: both upper- and lower-case letters
17
def printFileToScreen(fileName): f = open(fileName) text = f.read() f.close() print('The file contains:’) print() print(text)
18
Recursive stack is limited at 1000 calls by default Change this temporarily: import sys sys.setrecursionlimit(100000)
19
Test on 2 additional files Convert your files to plain text first Include the results at the top of your program
20
Name your files correctly Include docstring for your functions Test thoroughly Good luck
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.