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:

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:

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
password: hello
False
password: qwertyu123
True
password: a b c d e f g 09876
True
password: abcd1234567890
False
password: qwertyuiop
False
password: cs21rocks!!
True
password:
$

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
Please enter hit or stick...
--> 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...


4. Answer the Questionnaire

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.


Turning in your labs....

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.