# CS21 Lab 4: while loops and functions

## Due Saturday, October 7, before midnight

Make sure all programs are saved to your cs21/labs/04 directory!

### 1. Football/First Down Simulator

In American Football, the team on offense tries to move forward 10 yards, and they have at most four downs (plays) to do it. If they don't move forward at least 10 yards after four plays, the other team gets the ball. If they do move forward 10 yards (or more), they get to keep the ball. This is called a First Down.

For example:

• on their first play (down 1), let's say that the team gets 4 yards. They didn't go at least 10 yards in total, but they still have 3 more tries to get the remaining 6 yards.
• on down 2, let's say that the team gets pushed backwards 2 yards, so they "gain" -2 yards. Now the team has only moved forward a total of 2 yards (4 on the first try, -2 on the second try). Fortunately, they still have 2 more tries to get the remaining 8 yards.
• on down 3, let's say they get 15 yards. Now they have moved a total of 17 yards (4 on the first play, -2 on the second play, and 15 on this play). Since they have now moved forward at least 10 yards, they get a First Down.

In the example above, if on down 3 they only got 1 yard, then they'd have only moved forward 3 total yards (4 on the first try, -2 on the second try, 1 on the third try), and would still need 7 more yards. On down 4, if they only got 3 yards, then they would not have moved forward 10 total yards and would lose the ball (i.e., their total yards on all four downs was 4-2+1+3=6, which is less than 10).

Write a simulator (firstdown.py) that uses functions from the random library (either randrange() or choice()) to pick how many yards the team gets on each play. Your program should choose a random number between -5 and 15, inclusive. Based on the random number of yards chosen for each play, your simulator should show what the result is: either the team will try again to get 10 total yards, they will have made a First Down, or they have lost the ball. Your program should keep going until the team either gets a first down or, after using all 4 downs, loses the ball.

Here are a few examples:

$python3 firstdown.py Down #1: -2 yards ...12 yards to go Down #2: -4 yards ...16 yards to go Down #3: 11 yards ...5 yards to go Down #4: 4 yards ...1 yards to go Turnover on downs...$ python3 firstdown.py
Down #1: 5 yards
...5 yards to go
Down #2: -2 yards
...7 yards to go
Down #3: 14 yards
First Down!

$python3 firstdown.py Down #1: 10 yards First Down! In the first example above, the team only got 9 total yards (-2-4+11+4) on all four plays, so they lose the ball (Turnover on downs). In the second example, they got 17 yards (5-2+14) in three plays, so they get a new set of downs (back to first down). In the last example, they got 10 yards on the first play, so again they get a new set of downs. Hints: • use incremental development! :) • use a while loop. This may seem like a for loop (with 4 downs), but we would like you to use a while loop (i.e., the loop might run 4 times, but it also might end early if they get the 10 yards). • start by getting the while loop to work for one simple condition (e.g., keep picking random yardages until the total is 10 or more). Once you have that working, add in the rest of the problem. • this program does not need an extra function (i.e., just main() is enough) #### Extra Challenge (no points...just for fun, if you have time) Instead of picking randomly from numbers between -5 and 15, look into some of the other functions in the random library. Pick a more realistic distribution of yardages. For example, most NFL teams have a high chance of getting 2-4 yards, and a much smaller chance of getting 30+ yards in one play). ### 2. Checking Passwords Write a program called pwcheck.py to check if entered passwords are strong enough (i.e., have enough letters and non-letters, etc). For this program, start by writing a function called pwok(pw), that, given a string (pw), returns True if the password is OK, and False if it is not OK. To make things simple, we'll say a password is OK if it has at least 7 lowercase letters (a-z) and at least 1 digit (0-9). For example, if you put these in your main() function (just for testing), only the last one would print True: result = pwok("hello") print(result) result = pwok("hello123") print(result) result = pwok("swarthmore12345") print(result) ##### Add a while loop to main() Once you have the above function working, change the test code in main() to include a while loop. Your final program should keep asking the user for passwords to check, until the user enters an empty string (""). Here's an example of the final program (user input in bold): $ python3 pwcheck.py
False
True
password: a b c d e f g 09876
True
False
False
True
$ #### Extra Challenge (no points...just for fun, if you have time) Change the pwok function to accept two other arguments: minletters and mindigits. Then use these to check for passwords of different strengths. For example, calling pwok(pw,10,2) would only return True if the given password (pw) had at least 10 letters and at least 2 digits. ### 3. Simple Blackjack Blackjack is a card game where you are trying to get as close to 21 without going over. The player is initially dealt two cards, and then has the option to either get another card (hit) or not (stick). As long as the player takes another card and doesn't go over 21, they can continue (hit or stick). All cards have point values: Ace=1, Two=2, Three=3, ..., Ten=10, Jack, Queen, King all equal 10, and Ace can also be 11 points. So, for example, if my first two cards are the Two of Clubs and the King of Hearts, I've got 12 points so far. If I decide to hit, and my next card is the Eight of Diamonds (the suits don't really matter), then I've got 20 points, and I should probably stick with that. If, however, I decide to hit, and get the Ten of Clubs, then I've gone over 21 (2+10+8+10=30), and I lose. For this problem, we're going to write just the very first part of what could become a full game of blackjack. We'll just write what we have above (deal cards to player, ask if they want to hit or stick), and skip all the other details (i.e., no dealer to play against, and no betting). ##### Write getChoice() Write a function (in a file called blackjack.py) to get the user's choice: hit or stick. Your function should both ask for input and check to make sure the user enters a valid choice. If the user enters something invalid, the function should display an error message and ask again, until we get a valid choice. The function should also return the user's choice (a string), either "hit" or "stick". Here is an example of how it might look: --> pizza Please enter hit or stick... --> pony Please enter hit or stick... --> sticks Please enter hit or stick... --> stick In the above example, the function keeps looping until the user enters "stick", and then returns the string "stick". ##### Write main() Once you have written and tested your getChoice() function, now add a very simple version of blackjack: your main() function should deal two cards to the user and then loop until either the user goes over 21, or they decide to 'stick'. To make things simple, we'll just use numbers from 1-11 to represent cards. We'll also use the random library to deal cards to the user (i.e., don't worry about making a 52-card deck, just pick random numbers between 1 and 11 each time). Here are a few examples of how our simplified game might look (user input in bold): $ python3 blackjack.py
First card = 4
Second card = 10
Total so far = 14
--> hit
Next card = 3
Total so far = 17
--> stick
Player sticks with 17

$python3 blackjack.py First card = 9 Second card = 2 Total so far = 11 --> hit Next card = 4 Total so far = 15 --> hit Next card = 8 You lose... :( (total=23)$ python3 blackjack.py
First card = 9
Second card = 7
Total so far = 16
--> hit
Next card = 1
Total so far = 17
--> pony
--> hit
Next card = 4
Total so far = 21
--> stick
Player sticks with 21

#### Extra Challenge (no points...just for fun, if you have time)

Add in a dealer, and decide who wins.

Before dealing cards to the player, deal two cards to the dealer, showing only the "top" card:

\$ python3 blackjack.py
Dealer shows 10...
Player's First card = 9
Player's Second card = 2
Player's Total so far = 11
...

Then, after the player sticks, keep dealing cards to the dealer if the dealer has less that 16 points:

...
Player sticks with 20
-------------
Dealer has 13
Next card = 2
Dealer has 15
Next card = 6
Dealer has 21

Finally, decide who wins that hand, where a tie goes to the dealer. And if the user goes over 21, the dealer also wins...

Each lab has a short questionnaire at the end. Please edit the QUESTIONS-04.txt file in your cs21/labs/04 directory and answer the questions in that file.
Don't forget to run handin21 to turn in your lab files! You may run handin21 as many times as you want. Each time it will turn in any new work. We recommend running handin21 after you complete each program or after you complete significant work on any one program.