LESSON 07 – FUNCTIONS

 


WORK

TASK

Do the following practice quiz first.  The solutions are at the bottom of the practice quiz.

 

PROGRAM 1 – AVERAGE FUNCTION

Write a function called average that gets 4 numbers as parameters and returns the average of those four numbers.

 

Then call the function with the four marks that you hope to get in your courses this semester.

 

PROGRAM 2 – LOVE FUNCTION

Write a function called love that gets a string w as parameter.  It then outputs I love w.  For example, the call love(steak) will output I love steak.  However, if you simply call the function with love(), it should output I love Star Wars.  This will require that you use the default value.

 

PROGRAM 3 – IN RANGE FUNCTION

Write a function called in_range that gets 3 numbers as parameters returns True if the third number is between the first two numbers.  If it is not between the first two, the program returns False.

 

Test your function with different values.

 

PROGRAM 4 – PRINT EVEN FUNCTION

Write a function called print_even that gets a list as parameter and prints out all of the even numbers in the list.  Test your function.

 

PROGRAM 5 – DICE GAME PROGRAM (SMALL PROJECT)

 

PART 1

 

Write the function called rolldice that returns a random number between 1 and 6.

 

PART 2

 

In the main part of the program, simulate rolling 5 dice by calling the function five times and outputting the value of all die.  Make sure that you are rolling only numbers between 1 and 6 inclusively.

 

PART 3

 

Write another function called score that gets five parameters representing dice values and outputs a score based on the score rules below.

 

Score rules

 

The score of 5 different dice values will be the sum of those dice.

Example: If you rolled a 1, 2, 3, 5 & 6, then your score would be 17.

 

The score of doubles is actually doubled.

 

Example: 1,1,4,5,5 gives (1 + 1) * 2 + 4 + (5 + 5) * 2 = 28

 

Similarly, the score of triples is tripled.

 

Example: 1, 3, 4, 4, 4 gives 1 + 3 + (4 + 4 + 4) * 3 = 40

 

Quads are quadrupled.

 

Example: 2, 2, 2, 2, 5 gives (2 + 2 + 2 + 2) * 4 + 5 = 37

 

Quints are multiplied by five.

 

Example: 3, 3, 3, 3, 3 gives (3 + 3 + 3 + 3 + 3) * 5 = 75

 

In the case of both a double and a triple, we deal with both individually.

 

Example: 2, 2, 2, 5, 5 gives (2 + 2 + 2) * 3 + (5 + 5) * 2 = 38

 

The maximum score would be 6, 6, 6, 6, 6 which gives (6 + 6 + 6 + 6 + 6) * 5 = 150.

 

The lowest score would be 1, 2, 3, 4, 5 which gives 15.

 

 

Note that creating this function will be a challenge.  Perhaps placing the five dice rolls into a list and making use of the list.count(#) would be a reasonable solution.

 

PART 4

 

Back in the main part of the program, pass the results of the five rolled die in Part 2 to the score function.  It will return a score value and output it to screen.

 

Run this program many times to test that your score function is correctly calculating scores.

 

PART 5

 

Play a basic game.  Partner up with somebody.  You each run the progam and see your score. 

 

Alternately, you can do this on your own.

 

PART 6

 

To add an extra dimension to the game, the player that runs the program should have the opportunity to reroll the die if he/she is unhappy with the initial roll.  However, to stop the player from rolling over and over forever, the player loses one point for each reroll used.

 

PART 7

 

Play a few games with different people.  What is a decent score?  What is a good score?

 

PART 8 (TIME PERMITTING)

 

Make a new Python file.  Copy your code to the new file.  Alter the main part of the program so that it rolls six die and outputs the score of that roll.

 

Then, use a for loop to try this 100K times keeping a total of the overall score so that you can get an average score.

 

PART 9 (TIME PERMITTING)

 

Instead of the average score, come up with a way to calculate the median score. 

 

PART 10 (TIME PERMITTING)

 

With the knowledge acquired in Parts 8 and 9, you should be able to create a reasonable AI player for the game.  You can now make the game a “Beat the computer” game where the AI keeps the score as long as it is higher than average or the media.

 

Have fun!