12.6. Week 6 Lab

Material Covered

Conditionals, if, if-else, if-elif-else (Chapter 6)

12.6.1. Level 1

In this level you need to search through a string looking for certain key words.

  1. Underneath the starter code below, create a for loop that will iterate through every word in the key_word list. Make sure you give your iterator variable a good name, as you will need it to refer to each keyword as you iterate through the list.

  2. Inside the for loop create an if statement with a condition that checks to see if the current keyword is in the string paragraph.

    1. If the word is in paragraph, print a message to the console that the word has been found.

    2. Otherwise, print that the word was not found.

Tip

If you aren’t sure how to check whether a string is in another string, look at Section 6.4 in the textbook.

Output should look like this:

../_images/keyword_output.png

12.6.2. Level 2

In this level, you need to sort grades into categories of A, B, C, D and F. The grades need to be separated as follows: A >= 90, 90 > B >= 75, 75 > C >= 60, 60 > D >= 50, 50 > F

  1. Create a for loop that will iterate 50 times.

  2. In the for loop, create a variable that will be set to a random value from 0 to 100.

  3. Inside the for loop write code that will appropriately determine the letter grade that goes with the random numeric grade. You will want to use a chained conditional (if-elif-elif) type of structure to check for the different grade levels. For each level, print out the numeric and associated letter grade.

The end result should look something like this (this is shortened example):

../_images/grade_output.png

Tip

Reference the textbook Section 6.9 on chained conditionals if you don’t remember the syntax.

12.6.3. Level 3

In this level you will create a game of chance, similar to the card game war. Two players will have a random number generated from 1 to 52 (inclusive), the player with the higher number will win the point. In the case of a tie, a point is awarded to both players. 26 rounds will be played, after which the winner will be displayed.

  1. Start off by creating two variables to hold points, one for player 1 and one for player 2. Initialize these variables to 0.

  2. Create a for loop that will iterate for 26 rounds - note that we have created a constant for this, which you should use.

  3. Inside this loop, create two more variables (one for player 1’s card and one for player 2’s card) and assign them random values ranging from 1 to 52 (inclusive), using the randrange function. This represents the 52 cards in a standard deck of cards.

  4. Add a print statement that spritns out both players cards (see the samepl output below).

  5. Still inside the loop, create a chained conditional statement that responds to three possible states for this card round: if player 1’s card was higher, if player 2’s card was higher, or if it was a tie. Inside each branch of this chained conditional there should be a print statement printing out who got the point (see sample screenshot at the bottom of the level). Inside each branch, you also need to assign points to the winner of each round by incrementing player1’s points, or player2’s points. In the case of a tie, they should both get a point.

  6. After the for loop, we want to check to see who won the game. Similar to step 4, we need a chained conditional statement to check if player 1 has the most points, or if player 2 has the most points, or if it is a tie. Inside each branch of this chained conditional there should be a print statement printing out the winner and their final score (see sample photo at the bottom of the level)

An output example can be seen below (this is a shortened example)

../_images/card_rounds.png

Bonus: Organizing output is just as important as organizing code! Add round numbers before each round, a blank line after each round and indent the print statements for each round. (See sample output below)

Tip

Some useful characters for this bonus are: \n and \t, which are escape sequences that give you a newline or a tab.

../_images/card_rounds_formatted.png
You have attempted of activities on this page