Part 4 - Loops and Random Numbers

This section will teach programmers how they can repeat sections of code a number of times to complete repetitive tasks. It will also go over the basics on how to generate random numbers.

Back to main

What to do/hand in?

1 - Read through the Introduction Part 4: Looping and Random Numbers on programming arcade Games.

    • Be sure to actually run the example code, and attempt to implement the suggestions and answer the questions for each section.

    • There are three parts to this section that you must complete:

Part 1: Counted Loops, or the For Loop. You will need to read Chapter 4 sections 4.1 and 4.2 of Programming Arcade Games.

Part 2: Conditional Loops, or the While Loop. You will need to read Chapter 4 sections 4.3 and 4.4 of Programming Arcade Games.

Part 3: Random Numbers. You will need to read Chapter 4 section 4.5 of Programming Arcade Games.

3 - Complete the Chapter 4 Worksheet. Hand-in a single Python program that completes each of the requirements. Make sure that I can easily tell which sections of code solve each requirement in the worksheet. Use comments and/or print statements to separate each question.

4 - Complete your choice of one of the two game projects described in Part D.

5 - Upgrade your chatbot to version 1.2 using what you learned in this section, and the suggestions contained in the description found below.

Submit:

  1. Chapter 4 worksheet as a single Python program. Make sure to put your name in a comment at the top of your program!

  2. Your Choice of one of the two game projects: Guessing Game, and Paper Scissors Rock.

  3. Chatbot V1.2

      • Submit the final revision of your chatbot using what you learned in this chapter to make it indistinguishable from a human as described below.

Part 4 - Looping and Random Numbers.pdf

Worksheet

Part 4 - Loops Worksheet.pdf

Chatbot V1.2

Part 4 - Chatbot v1.2.pdf

Hints and FAQ's

1 - When you import a library into your program, you should only include your import statement once in your program. All of your import statements should be at the top of your program.

You can generate random numbers as often as you like, but should only have the line of code 'import random' once at the top of your program.


2 - If you wan't the probability of a player being successful at a particular decision point to be more than 50%, try generating a random number in a larger range. For example if you want a 75% chance of winning, generate a number in the range of 1 to 4 (inclusive) and test for the losing value. Here is an example:

number = random.randint(1, 4):

if number == 1:

#Here is the losing code because there is a 1 in 4 chance of the random number being 1

else:

#Here is the winning code

How could you change the code above for an 80% chance of winning?

FAQ

Q: What is the point of using an Else block in a loop? Couldn't I just put that same code outside the loop?

For example:

for i in range(1, 4):

print(i)

else:

print("See, I can count to three.")


This prints:

1

2

3

See, I can count to three.

for i in range(1, 4):

print(i)

print("See, I can count to three.")


This prints:

1

2

3

See, I can count to three.

A: In the vast majority of situations the Else is not necessary. The code in the Else block gets run whenever the For loop ends normally. If you use a break to exit the For loop early, the Else block will not be executed.

See Below:

for i in range(1, 4):

print(i)

break # we exit the loop early

else:

print("See I can count to three.")


This prints:

1

for i in range(1, 4):

print(i)

break # we exit the loop early

print("See, I can count to three")


This prints:

1

See, I can count to three.

For a more detailed explanation click here.