curriculum.naf.orgcurriculum.naf.org/packaged/assets/downloads/technolog…  · web viewwhen you...

52
AOIT Introduction to Programming Lesson 8 Simple Conditional Branching and Looping Student Resources Resource Description Student Resource 8.1 Planning: New Password Program Student Resource 8.2 Design and Coding: New Password Program Student Resource 8.3 Reading: Looping Using For-Loops Student Resource 8.4 Notes and Practice: Looping Using For-Loops Student Resource 8.5 Testing: New Password Program Student Resource 8.6 Reading: Looping Using While-Loops Student Resource 8.7 Notes and Practice: Looping Using While-Loops Student Resource 8.8 Design and Coding: Guess-A-Number Program Student Resource 8.9 Testing: Guess-A-Number Program Copyright © 2009–2015 NAF. All rights reserved.

Upload: others

Post on 08-Sep-2020

0 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: curriculum.naf.orgcurriculum.naf.org/packaged/assets/downloads/technolog…  · Web viewWhen you log in to an account on a computer or a website, you enter a user ID and password

AOIT Introduction to Programming

Lesson 8Simple Conditional Branching and

LoopingStudent Resources

Resource Description

Student Resource 8.1 Planning: New Password Program

Student Resource 8.2 Design and Coding: New Password Program

Student Resource 8.3 Reading: Looping Using For-Loops

Student Resource 8.4 Notes and Practice: Looping Using For-Loops

Student Resource 8.5 Testing: New Password Program

Student Resource 8.6 Reading: Looping Using While-Loops

Student Resource 8.7 Notes and Practice: Looping Using While-Loops

Student Resource 8.8 Design and Coding: Guess-A-Number Program

Student Resource 8.9 Testing: Guess-A-Number Program

Copyright © 2009–2015 NAF. All rights reserved.

Page 2: curriculum.naf.orgcurriculum.naf.org/packaged/assets/downloads/technolog…  · Web viewWhen you log in to an account on a computer or a website, you enter a user ID and password

AOIT Introduction to ProgrammingLesson 8 Simple Conditional Branching and Looping

Student Resource 8.1

Planning: New Password ProgramStudent Names:_____________________________________________________ Date:____________

Directions: In this lesson, you will be writing a program to enforce password rules on a proposed new password typed in by a user. To help you start planning that program, you will be doing some exercises involving password rules and guidelines.

Follow the specific directions in each of the sections below.

Categorizing Password Rules and GuidelinesWhen you log in to an account on a computer or a website, you enter a user ID and password. If the password is too simple in form, somebody else might be able to guess what it is and log in to your account and have access to information you would like to keep private. To prevent this from happening, the password you choose has rules applied to it. For example, you may be required to choose a password that is at least a certain length, or that contains certain types of characters.

In this lesson, you will write a Python program that tests a password string to make sure it follows rules that are reasonable and rules that are “enforceable” by Python.

Categorize the following password rules and guidelines as:

● Rules you believe can clearly be enforced in a Python program (for example, there is a mechanism in Python to tell whether user input is numeric or not, and a way to reject the user input if it is not numeric)

● Rules you believe can possibly be enforced in a Python program (this means that you may not know how to do this in Python today, but based on your current programming skill and knowledge, you think it is possible)

● General guidelines that probably would not be enforceable but could be suggested as best practices to computer users (for example, for security reasons it is not good for people to use their sister’s name as a password, but there is unlikely to be an easy way for you as a programmer to find out a specific girl’s name and put it in a place where a Python program could find and reject it)

Indicate the category by putting an X in the appropriate column.

Two of the categorizations have been done for you.

Rule, Guideline Clearly Enforceable Rule

Possibly Enforceable Rule

General Guideline

At least one character in the password must be numeric. X

The password is not a word found in a dictionary.

The password must not be a name easy to guess (for example, someone in your family, a close friend, or a pet).

X

Copyright © 2009–2015 NAF. All rights reserved.

Page 3: curriculum.naf.orgcurriculum.naf.org/packaged/assets/downloads/technolog…  · Web viewWhen you log in to an account on a computer or a website, you enter a user ID and password

AOIT Introduction to ProgrammingLesson 8 Simple Conditional Branching and Looping

The password must contain at least eight characters.

The password must not be a letter sequence found on most keyboards (for example, qwerty).

The characters must be alphanumeric (no special characters like # or $).

The password must not be one the user has used before.

Alphabetic characters in the password must be a mixture of lowercase and uppercase.

The password must not be a short word doubled to make a longer string.

At least one character in the password must be alphabetic.

Categorizing Valid and Invalid PasswordsHere is a list of proposed passwords:

● cat

● five$foryou

● alligator

● a5555555

● September25

● SamanthaJones

● Samantha_Jones

● mouse678

● RiV3R11

Apply the rules in the first column of the following table, in the order they are presented (1–4), to each of the proposed passwords. Write each of the proposed passwords in the appropriate box so that an appropriate message about the password would be sent to the user.

For example, the first proposed password is “cat.” If you apply rule 1 to “cat,” you find that it does not follow the rule (or “meet the condition,” as it is usually stated in programming). So, you would put it in the “If No” box in the top row, as shown.

One other example has been done for you.

Note: There can be more than one password in a given box.

Rule, Condition Password Meets the Password Does Not Meet the

Copyright © 2009–2015 NAF. All rights reserved.

Page 4: curriculum.naf.orgcurriculum.naf.org/packaged/assets/downloads/technolog…  · Web viewWhen you log in to an account on a computer or a website, you enter a user ID and password

AOIT Introduction to ProgrammingLesson 8 Simple Conditional Branching and Looping

Condition Condition

1. Password must contain at least eight characters.

If Yes, continue with condition 2.

If No, send “password invalid” message.

cat

2. The characters must be alphanumeric (no special characters like # or $).

If Yes, continue with condition 3.

If No, send “password invalid” message.

five$foryou

3. At least one character must be alphabetic.

If Yes, continue with condition 4.

If No, send “password invalid” message.

4. At least one character must be numeric.

If Yes, send “password accepted” message.

If No, send “password invalid” message.

Deciding Which Rule to Apply FirstWhy do you think the rules were listed in the order (1–4) that they appear in the previous table?

The order the rules are listed (or “the order the conditions are applied” in the Python program) may seem arbitrary to you at first, but it is important. The logic will become clearer to you as you gain programming experience. For now, look at the order and answer the following questions:

1. Why would you apply the rule “characters must be alphanumeric” before the rule “at least one character must be alphabetic”?

2. Why would you apply the rule “at least one character must be alphanumeric” after the rule “password must contain at least eight characters”?

Enforcing Password RulesIf you were writing a Python program to enforce password rules, what “tools” would you use?

Copyright © 2009–2015 NAF. All rights reserved.

Page 5: curriculum.naf.orgcurriculum.naf.org/packaged/assets/downloads/technolog…  · Web viewWhen you log in to an account on a computer or a website, you enter a user ID and password

AOIT Introduction to ProgrammingLesson 8 Simple Conditional Branching and Looping

Sometimes string methods can be used for this purpose. One of the string methods you have been introduced to in this course is string.isalnum(). Which rule in the previous table do you think it applies to?

Copyright © 2009–2015 NAF. All rights reserved.

Page 6: curriculum.naf.orgcurriculum.naf.org/packaged/assets/downloads/technolog…  · Web viewWhen you log in to an account on a computer or a website, you enter a user ID and password

AOIT Introduction to ProgrammingLesson 8 Simple Conditional Branching and Looping

Student Resource 8.2

Design and Coding: New Password ProgramStudent Names:_____________________________________________________ Date:____________

Directions: Write the design for the New Password program, and then code it.

You will design and code the program in three stages. Follow the specific directions in each part below.

Part 1: Basic ProgramFollow the instructions below to design and code the basic version of the New Password program.

Problem StatementWrite a program that prompts the user for a proposed new password and then tests to be sure it passes these password rules:

● The password must be at least eight characters in length.

● It must contain only alphanumeric characters.

● It must contain at least one letter.

● It must contain at least one number.

In the basic version of the program, you will test only one rule: the password must be at least eight characters. In this version, the user gets only one try to create a valid password (in later versions, the user gets three tries).

Requirements and Program Design (Algorithm)General Requirements

● Open a new window in Python IDLE.

● Create the New Password program skeleton with the sections shown in the table below, name the program new_password.py, and save it in your Lesson 8 folder.

Program Section Notes

################################ PROLOG SECTION# new_password.py# Program to check a user's proposed# password.# (today's date goes here)# (programmer names go here)# (tester names go here)# Possible future enhancements:# Unresolved bugs:

This section contains important information about the program.

Notice that everything in this section is included as a Python comment.

You may not be able to complete this section before you code or test the program. For example, you would list unresolved bugs right before you hand in the program for

Copyright © 2009–2015 NAF. All rights reserved.

Page 7: curriculum.naf.orgcurriculum.naf.org/packaged/assets/downloads/technolog…  · Web viewWhen you log in to an account on a computer or a website, you enter a user ID and password

AOIT Introduction to ProgrammingLesson 8 Simple Conditional Branching and Looping

############################### assessment. Better yet, you won’t have any unresolved bugs!

################################ PROCESSING INITIALIZATION SECTION###############################

# code goes here

In this section (below this header) is setup information for the program.

Examples: code to set or initialize variables, and code to print general information to the user

################################ PROCESSING SECTION# Branching code:# Looping code:###############################

# code goes here

This section contains the main processing code.

Examples: branching and looping code

################################ CLEANUP, TERMINATION, and EXIT # SECTION###############################

# code goes here

Here’s where you’ll put the final messages to the user.

Examples: “Your new password is valid” or “Your new password is not valid”

Programming Elements to Use as Building BlocksYou will be using the programming elements in the following table as building blocks to create pseudocode or, if possible, actual Python code. (Pseudocode can be inserted in the code as comment statements, but its main purpose is to aid in the design process.)

First read through the information in the following table, and then use the building blocks as you design your program.

Type Name or Value Notes, Code Examples

Variable minlength

valid

password

You will be assigning values to each of these variables.

Assignment statement = minlength =

Numeric literal 8 minlength = 8

Boolean constant True or False valid = False

Input function input() You already know how to use this function.

Function len() Stands for “length.”

len(password) …

Decision statement if if len(password) … :

Copyright © 2009–2015 NAF. All rights reserved.

Page 8: curriculum.naf.orgcurriculum.naf.org/packaged/assets/downloads/technolog…  · Web viewWhen you log in to an account on a computer or a website, you enter a user ID and password

AOIT Introduction to ProgrammingLesson 8 Simple Conditional Branching and Looping

else else:

Comparison operator ==

>=

“Is equal to”

“Is greater than or equal to”

Logical operator not

Processing Initialization SectionWrite code or pseudocode for your processing initialization section using the building blocks in the table above. One of the statements has been done for you.

● Set the minimum password length.

minlength = 8

● Start the password out as invalid (set the valid initialization flag as False). Write the code or pseudocode for this below.

Processing SectionIn the processing section of the program, you need to prompt for the user’s new password and then test it against the rule that a password must contain at least eight characters. If the password does not meet the criterion, print a meaningful error message so that the user will know what he or she did wrong.

Python requires that branching code be indented very precisely. The following table shows you how the branching code needs to look.

Using the building blocks in the previous table, fill in this table with code or pseudocode statements. Most of the statements have been done for you.

# get the user input

# test the password length

if len(password) >= minlength:

# if the password meets the condition, set valid to truevalid = True

else:

# otherwise, give the user a meaningful error messageprint("Error, password is less than",minlength,"characters.")

Copyright © 2009–2015 NAF. All rights reserved.

Page 9: curriculum.naf.orgcurriculum.naf.org/packaged/assets/downloads/technolog…  · Web viewWhen you log in to an account on a computer or a website, you enter a user ID and password

AOIT Introduction to ProgrammingLesson 8 Simple Conditional Branching and Looping

Cleanup, Termination, and Exit SectionIn this section, you need to tell the user whether the proposed password is valid. Using the building blocks from the table on the previous page, fill in this table with code or pseudocode statements. Most of the statements have been done for you.

# print informational messages

# if the password meets the condition (at least 8 characters)if valid = True:

# print the "successful" messageprint("Your new password is valid.")

else:

# otherwise, print the "unsuccessful" messageprint("Your new password is not valid.")

Program ImplementationFollowing specific instructions from your teacher, code and run the basic version of New Password.

Part 2: Intermediate ProgramFollow the instructions below to design and code the intermediate version of New Password.

Problem StatementTo write the intermediate version of the New Password program, you will modify some code you wrote for the basic version and add some new code.

In the intermediate version of the program, you will test all the password rules:

● The password must be at least eight characters in length.

● It must contain only alphanumeric characters.

● It must contain at least one letter.

● It must contain at least one number.

You need to print out the password rules for the user. The user still gets only one try to create a valid password. For specific instructions, see the “Requirements and Program Design (Algorithm)” section below.

Requirements and Program Design (Algorithm)Processing SectionIn the basic version of your program, you tested for only one condition: password length. In the intermediate version of the program, you need to test for all four conditions.

In the following table, the code for the second condition (the password must be all alphanumeric—no special characters like # or $) has been added. Carefully read through the code with your programming partner, and then answer the questions below the table.

Copyright © 2009–2015 NAF. All rights reserved.

Page 10: curriculum.naf.orgcurriculum.naf.org/packaged/assets/downloads/technolog…  · Web viewWhen you log in to an account on a computer or a website, you enter a user ID and password

AOIT Introduction to ProgrammingLesson 8 Simple Conditional Branching and Looping

# test the password lengthif len(password) >= minlength:

# test for all alphanumericif password.isalnum():

# if the password meets BOTH conditions, set valid to truevalid = True

else:

# otherwise, give the user a meaningful error messageprint("Error, password is not alphanumeric.")

else:

# otherwise, give the user a meaningful error messageprint("Error, password is less than",minlength,"characters.")

Answer the following questions in the space provided:Describe the indentation of the line of code that does the alphanumeric test. Why do you think that line is indented the way it is?

Why does the valid = True line of code come after the test for alphanumeric input?

What is the error message that “matches” the test for alphanumeric input?

When you add the third test (“at least one letter”), what will you need to do with the valid = True line of code? Will it be at the same indentation level it is now?

When you add the fourth test (“at least one digit”), how many times will the line valid = True need to be indented?

What character do you need to remember to put after every if-statement and else-statement?

Copyright © 2009–2015 NAF. All rights reserved.

Page 11: curriculum.naf.orgcurriculum.naf.org/packaged/assets/downloads/technolog…  · Web viewWhen you log in to an account on a computer or a website, you enter a user ID and password

AOIT Introduction to ProgrammingLesson 8 Simple Conditional Branching and Looping

Why do you think the tests have been written in the order they have (specifically, why does the test for alphanumeric data come before the test for alphabetic data)?

Mark the following statements as true or false:

Statement True False

A matching else-statement always needs to immediately follow its if-statement in the program sequence.

Matching if- and else-statements need to be indented to the same level.

The valid = True statement does not execute unless all the tests are passed.

The valid = True statement follows all the else-statements.

The purpose of all the else-statements in this branching sequence is to print meaningful error messages to the user.

When you have coded all four tests, the pattern of your lines of code will look like an arrow pointing to the right.

Write the branching code for all four conditions: Write the comments and code for each of the four tests in the space below. Use the back of this page if necessary.

Program ImplementationFollowing specific instructions from your teacher, code and run the intermediate version of New Password.

Copyright © 2009–2015 NAF. All rights reserved.

Page 12: curriculum.naf.orgcurriculum.naf.org/packaged/assets/downloads/technolog…  · Web viewWhen you log in to an account on a computer or a website, you enter a user ID and password

AOIT Introduction to ProgrammingLesson 8 Simple Conditional Branching and Looping

Part 3: Advanced ProgramFollow the instructions below to design and code the advanced version of the New Password program.

Problem StatementIn the advanced version of the program, you continue to test all the password rules. You need to add general information and an explanation of the password rules for your user. The user gets three tries to set the password. To give the user three tries, you need to create a for-loop.

For specific instructions, see the “Requirements and Program Design (Algorithm)” section below.

Requirements and Program Design (Algorithm)Processing Initialization SectionWrite general information and instructions to your user:

● Write a draft of your information statements and password rules below. You need to state (1) what the program does, (2) how many tries the user gets, and (3) what the four rules are.

● As you write, pretend you are talking directly to your user (for example, “Your password must contain at least one digit”).

Set the number of tries to three:

● Use the valid = False statement as a model. In that statement you are setting the variable valid to the value False.

● How would you set the value of the tries variable to 3? Write your statement below.

Processing SectionYou have already defined the number of tries (three) and put it into the processing initiation section of your program.

Now you need to follow these steps:

● At the top of the test section of the code, you will be adding the for-loop, specifically, for i in range(tries):, which means “for integer i in the range of integers up to the number of tries (three), loop through the code that is indented below this loop.” Python keeps track of the counter (i), so it knows how many times the loop code has been repeated.

Write the comment and code for the loop below.

Copyright © 2009–2015 NAF. All rights reserved.

Page 13: curriculum.naf.orgcurriculum.naf.org/packaged/assets/downloads/technolog…  · Web viewWhen you log in to an account on a computer or a website, you enter a user ID and password

AOIT Introduction to ProgrammingLesson 8 Simple Conditional Branching and Looping

● After the valid = True statement, add a break statement so that after the program goes through the tries code three times, it will leave the loop code and proceed to the “report the final status” section.

The break statement is simply the word break.

Write yourselves a note to remember to add the break statement.

Program ImplementationFollowing specific instructions from your teacher, code and run the advanced version of New Password.

Make sure your assignment meets or exceeds the following assessment criteria:Program Package

• Contains all components: (1) New Password design and coding worksheet (this document), (2) a printout of the New Password program, (3) the New Password testing worksheet, and (4) new_password.py program in the Lesson 8 folder.

Design Document

• The algorithm represents a complete solution to the problem statement.

• The algorithm contains an appropriate level of detail for the target audience and contains both major and minor steps in the correct order.

• The document is neat and legible and does not contain spelling or grammatical errors.

Test Document

• The document contains a comprehensive set of test cases appropriate for the program objectives.

• The document is neat and legible and does not contain spelling or grammatical errors.

Program Printout

• The prolog contains the information specified in the design document.

• The program comments accurately describe the Python statements and are detailed enough so that peer students would be able to understand the entire program.

• The comments do not contain spelling or grammatical errors.

Python Code

Copyright © 2009–2015 NAF. All rights reserved.

Page 14: curriculum.naf.orgcurriculum.naf.org/packaged/assets/downloads/technolog…  · Web viewWhen you log in to an account on a computer or a website, you enter a user ID and password

AOIT Introduction to ProgrammingLesson 8 Simple Conditional Branching and Looping

• The program meets the requirements stated in the design document.

• The program runs without errors and produces the intended results.

Copyright © 2009–2015 NAF. All rights reserved.

Page 15: curriculum.naf.orgcurriculum.naf.org/packaged/assets/downloads/technolog…  · Web viewWhen you log in to an account on a computer or a website, you enter a user ID and password

AOIT Introduction to ProgrammingLesson 8 Simple Conditional Branching and Looping

Student Resource 8.3

Reading: Looping Using For-Loops

This presentation introduces the programming concept of looping and provides examples of for-loops.

Copyright © 2009–2015 NAF. All rights reserved.

Page 16: curriculum.naf.orgcurriculum.naf.org/packaged/assets/downloads/technolog…  · Web viewWhen you log in to an account on a computer or a website, you enter a user ID and password

AOIT Introduction to ProgrammingLesson 8 Simple Conditional Branching and Looping

If we didn’t have loops in programming languages, each repeated sequence would require new code.

Counting to a million would be very time-consuming and would require a prohibitive amount of code!

Copyright © 2009–2015 NAF. All rights reserved.

Page 17: curriculum.naf.orgcurriculum.naf.org/packaged/assets/downloads/technolog…  · Web viewWhen you log in to an account on a computer or a website, you enter a user ID and password

AOIT Introduction to ProgrammingLesson 8 Simple Conditional Branching and Looping

This simple loop example in Python prints three numbers.

Code analysis:

• The first line of the program (“for i in range(3):”) is called the for-loop header.

• A for-loop always begins with the keyword for.

• After that comes the loop variable (i).

• Next is the keyword in.

• After that is the range() function and a terminating token (:).

• The “print(i)” line is the loop’s body, which is repeated i times.

Answer:

• The line “print(i)” gets repeated three times.

Copyright © 2009–2015 NAF. All rights reserved.

Page 18: curriculum.naf.orgcurriculum.naf.org/packaged/assets/downloads/technolog…  · Web viewWhen you log in to an account on a computer or a website, you enter a user ID and password

AOIT Introduction to ProgrammingLesson 8 Simple Conditional Branching and Looping

This simple for-loop example prints 10 numbers.

The loop header must always end with a colon (:). The loop body must always be indented. If the loop body had multiple lines, they would all need to be indented the same number of spaces. You can use the tab to indent lines, or you can indent manually using the spacebar.

Answers:

• The first number that gets printed is 0.

• The last number that gets printed is 9.

To most nonprogrammers or beginning programmers, it seems illogical to print the numbers 0 to 9, rather than 1 to 10. This is actually a historical legacy that comes from the C programming language. Starting numbering at 0 (by default) is common in programming, but it is not universal in all languages. When you start using a new programming language, you need to verify whether numbering starts with 0.

Copyright © 2009–2015 NAF. All rights reserved.

Page 19: curriculum.naf.orgcurriculum.naf.org/packaged/assets/downloads/technolog…  · Web viewWhen you log in to an account on a computer or a website, you enter a user ID and password

AOIT Introduction to ProgrammingLesson 8 Simple Conditional Branching and Looping

The program that printed 0 through 9 has been replaced by one that prints 1 through 10.

Answers:

• The first number that gets printed is 1.

• The last number that gets printed is 10.

• You can include multiple lines of code in a loop body. You can include branching sequences and additional loops.

Copyright © 2009–2015 NAF. All rights reserved.

Page 20: curriculum.naf.orgcurriculum.naf.org/packaged/assets/downloads/technolog…  · Web viewWhen you log in to an account on a computer or a website, you enter a user ID and password

AOIT Introduction to ProgrammingLesson 8 Simple Conditional Branching and Looping

In this program, the count is determined by user input.

Because input() data is string by default, we convert it to integer data before storing it in the count variable.

In the instructions to the user, we are encouraging a reasonable range (so that the numbers printed will be somewhere between 1 and 20); otherwise, we would get tired of watching Python print numbers.

Answers:

• The line “print(i)” gets repeated. We don’t know how many times until we get the user input, but by the time we get to the loop, we have a value for the count variable.

• We don’t want the user to enter a number that would cause the program to take too much time to execute the loop.

Copyright © 2009–2015 NAF. All rights reserved.

Page 21: curriculum.naf.orgcurriculum.naf.org/packaged/assets/downloads/technolog…  · Web viewWhen you log in to an account on a computer or a website, you enter a user ID and password

AOIT Introduction to ProgrammingLesson 8 Simple Conditional Branching and Looping

This presentation has introduced for-loops in preparation for the final (advanced) stage of the New Password program, which contains a for-loop.

Still to be covered (in a later presentation) are while-loops.

Copyright © 2009–2015 NAF. All rights reserved.

Page 22: curriculum.naf.orgcurriculum.naf.org/packaged/assets/downloads/technolog…  · Web viewWhen you log in to an account on a computer or a website, you enter a user ID and password

AOIT Introduction to ProgrammingLesson 8 Simple Conditional Branching and Looping

Student Resource 8.4

Notes and Practice: Looping Using For-LoopsStudent Names:_____________________________________________________ Date:____________

Directions: While you are watching the “Looping Using For-Loops” presentation, take notes and answer the questions in Part 1, below. Then do the exercises in Part 2 in the Python shell, and record the results and answer the questions in the space provided.

Part 1: Notes and Analysis1. What are loops in programming?

2. How do loops help programmers save time and effort in writing programs?

3. What does a Python for-loop do?

4. What are the two major parts of a for-loop? Write an example of each.

5. If you have a loop header for i in range(10):, where does the range begin: 0 or 1? Where does the range end?

6. How would you change the loop header in question 5 to print the numbers 1 through 10? Write the loop header code below.

7. Here is the program in slide 6 of the presentation:

# count as high as the user wants

count = int(input ("How high do you want to count? (no lower than 1, no higher than 21) "))

for i in range(1,count+1):

print(i)

1. In the space below, write a similar program that sets the value of count to 6 (rather than taking input from the user).

Copyright © 2009–2015 NAF. All rights reserved.

Page 23: curriculum.naf.orgcurriculum.naf.org/packaged/assets/downloads/technolog…  · Web viewWhen you log in to an account on a computer or a website, you enter a user ID and password

AOIT Introduction to ProgrammingLesson 8 Simple Conditional Branching and Looping

Part 2: Independent PracticeOpen Python IDLE and create a new program that uses a for-loop. Here are the requirements:

● Name your program numbers_squared.py and save it in your Lesson 8 folder.

● Set a variable named count to 11.

● Create a for-loop to print the numbers 1 through the value of count, along with their squares.

● Hint: You learned what operator to use to square a number in Lesson 6.

● Hint: You can print i and i-squared with a single line of code.

● Write your code in the space below before you start to type it into your Python program.

● When you get the first version of your program running, change your print statement so that the output reads as “1 squared is 1,” “2 squared is 4,” and so on. Hint: Again, you need only a one-line print statement to accomplish this.

Copyright © 2009–2015 NAF. All rights reserved.

Page 24: curriculum.naf.orgcurriculum.naf.org/packaged/assets/downloads/technolog…  · Web viewWhen you log in to an account on a computer or a website, you enter a user ID and password

AOIT Introduction to ProgrammingLesson 8 Simple Conditional Branching and Looping

Student Resource 8.5

Testing: New Password ProgramStudent Names:_____________________________________________________ Date:____________

Directions: This is the test plan for your New Password program. Testing of your program will be done by your testing partners using this plan as a guide. Test plans are very important in the programming world. When you interview for an internship or programming job, you should tell the interviewer that you have had experience writing and implementing test plans.

This plan consists of three tables, each of which contains three columns. The first two columns need to be filled in by you, the programmer (or coder). The third column will be filled in by your testing partners as they test your program.

A lot of the information in the first and second columns has already been filled in for you. Complete the plan by doing the following:

1. Fill in any missing information in the first column of all three tables.

2. Test your own program by following the instructions in column one and filling in the second column. Fix any errors you find. (In the programming world, programmers are expected to do their own preliminary testing before giving their program to the test team.)

3. When you are confident your program will pass all the tests, give your program to your testers.

Branching Code TestsTest Coder: Expected Response Tester: Yes/No or Comment

In response to the password prompt…

1. Enter: catRule being tested: Password must be eight characters or longer.

Fail with “minlength” error message

2. Enter: five$foryouRule being tested:

3. Enter:

Rule being tested: At least one character must be alphabetic.

4. Enter:

Rule being tested:

5. Enter: alligator9 Pass with final “successful” message

Copyright © 2009–2015 NAF. All rights reserved.

Page 25: curriculum.naf.orgcurriculum.naf.org/packaged/assets/downloads/technolog…  · Web viewWhen you log in to an account on a computer or a website, you enter a user ID and password

AOIT Introduction to ProgrammingLesson 8 Simple Conditional Branching and Looping

6. Enter: Pass with final “successful” message

Looping Code TestsTest Coder: Expected Response Tester: Yes/No or Comment

In response to the password prompt…

7. Enter a valid password on the first try.

Break out of the loop, print “valid password” message, and exit

8. Enter an invalid password on the first try and a valid password on the second try.

Environmental TestsTest Coder:

Yes/NoTester: Yes/No or Comment

Program Execution

9. The program executes without runtime errors.

Initial Messages

10. As the program begins to execute, it prints a clear informational message explaining what is happening and what the user needs to do.

11. The program prints the password rules in clear, understandable language.

12. None of the initial messages contain spelling or grammatical errors.

Final Messages

13. Before the program exits, it prints a clear message telling the user whether the password was accepted.

14. The final messages contain no spelling or grammatical errors.

Copyright © 2009–2015 NAF. All rights reserved.

Page 26: curriculum.naf.orgcurriculum.naf.org/packaged/assets/downloads/technolog…  · Web viewWhen you log in to an account on a computer or a website, you enter a user ID and password

AOIT Introduction to ProgrammingLesson 8 Simple Conditional Branching and Looping

Student Resource 8.6

Reading: Looping Using While-Loops

This presentation introduces the concept of while-loops and provides several examples.

Copyright © 2009–2015 NAF. All rights reserved.

Page 27: curriculum.naf.orgcurriculum.naf.org/packaged/assets/downloads/technolog…  · Web viewWhen you log in to an account on a computer or a website, you enter a user ID and password

AOIT Introduction to ProgrammingLesson 8 Simple Conditional Branching and Looping

As a reminder from the prior presentation (on for-loops), a loop is a programming structure that repeats the block of code that “belongs” to it. In Python the block of code must be indented below the loop header.

Python for-loops repeat the block of code a specified number of times.

While-loops repeat the block of code while a certain condition is true.

In the example, assuming the current value of a is 5 and b is 3, the test evaluates to true, so the value of a gets printed.

If a were currently 3 and b were currently 5, the value of a would not get printed.

Note that if the values never change, a continues to get printed. In programming this is called an infinite loop, and it is never what the programmer intends to happen.

An infinite loop is a loop in a program that can never end. Once an infinite loop is entered, the program “freezes” and in some cases also freezes the computer it is running on, using up all the available computing cycles. If there’s a write to a file inside the loop, eventually this single piece of code could consume the entire system’s hard drive space. The most common kind of infinite loop is a while-loop with a condition that is always true. The program loops until some condition is satisfied, and that can never happen.

For more information about infinite loops, see the QuickStart Guide.

Copyright © 2009–2015 NAF. All rights reserved.

Page 28: curriculum.naf.orgcurriculum.naf.org/packaged/assets/downloads/technolog…  · Web viewWhen you log in to an account on a computer or a website, you enter a user ID and password

AOIT Introduction to ProgrammingLesson 8 Simple Conditional Branching and Looping

This is a while-loop that prints out 10 numbers (0 to 9).

The first statement, “i = 0”, is an initialization statement. Whereas for-loops automatically initialize their loop variable, while-loops must be explicitly initialized.

A while-loop keeps executing the block until the while-loop condition is false.

Answers:

• The first time through the loop the condition is true.

• The 10th time through the loop the condition is false, and the loop will not execute the block.

Copyright © 2009–2015 NAF. All rights reserved.

Page 29: curriculum.naf.orgcurriculum.naf.org/packaged/assets/downloads/technolog…  · Web viewWhen you log in to an account on a computer or a website, you enter a user ID and password

AOIT Introduction to ProgrammingLesson 8 Simple Conditional Branching and Looping

This flowchart shows how the while-loop works. (A for-loop works essentially the same way; the main difference is that in a for-loop, the i = i + 1 step is hidden.)

The statement “i = 0” is the initializer. The diamond-shaped box contains a question that leads to a yes or no decision. If the “no” branch is taken, the program exits the loop. If the “yes” branch is taken, the block of code within the loop is executed.

After i is incremented, the condition is evaluated again.

The loop continues to execute until i reaches 10. If there were more code below the loop, it would be executed. In this program, there is none.

Copyright © 2009–2015 NAF. All rights reserved.

Page 30: curriculum.naf.orgcurriculum.naf.org/packaged/assets/downloads/technolog…  · Web viewWhen you log in to an account on a computer or a website, you enter a user ID and password

AOIT Introduction to ProgrammingLesson 8 Simple Conditional Branching and Looping

This while-loop also prints out 10 numbers.

The initialization statements assign values to variables a and b.

Each time the program goes through the loop, a increases its value by one. When it reaches 10, the loop condition is no longer true, and the program drops out of the loop.

Answers:

• Ten numbers will be printed.

• The numbers printed are 0 through 9, with each number printing on a new line. To print all the numbers on a single line delimited by spaces: print(a, end = " ")

• The value of b does not change.

Copyright © 2009–2015 NAF. All rights reserved.

Page 31: curriculum.naf.orgcurriculum.naf.org/packaged/assets/downloads/technolog…  · Web viewWhen you log in to an account on a computer or a website, you enter a user ID and password

AOIT Introduction to ProgrammingLesson 8 Simple Conditional Branching and Looping

This while-loop has four variables, which are all initialized in the first line of code.

Both conditions (a < b and c != d) must be true for the loop block to be executed.

Answers:

• To “decrement by one” means to subtract one.

• The loop executes five times.

• The numbers printed are 0, 1, 2, 3, and 4.

Copyright © 2009–2015 NAF. All rights reserved.

Page 32: curriculum.naf.orgcurriculum.naf.org/packaged/assets/downloads/technolog…  · Web viewWhen you log in to an account on a computer or a website, you enter a user ID and password

AOIT Introduction to ProgrammingLesson 8 Simple Conditional Branching and Looping

In this program, the while-loop keeps executing until the user answers yes or no. For any other answer, the loop re-executes, which means the user gets asked over and over again, “Is the sky falling?”

Other than giving a yes or no answer, the only way to break out of the loop (and stop program execution) is Ctrl+C.

When the user finally does give a yes or no answer, the program prints “Thank you!”

Answers:

• If the user answers with “nonsense,” the loop continues to execute and the program continues to ask the question.

• If the user answers yes, the program breaks out of the loop, and the final print statement executes (that is, the program prints “Thank you!”).

Copyright © 2009–2015 NAF. All rights reserved.

Page 33: curriculum.naf.orgcurriculum.naf.org/packaged/assets/downloads/technolog…  · Web viewWhen you log in to an account on a computer or a website, you enter a user ID and password

AOIT Introduction to ProgrammingLesson 8 Simple Conditional Branching and Looping

This is another way to do the Sky-Is-Falling program using a while-loop.

The two versions of the program illustrate how versatile the while-loop is in Python.

Copyright © 2009–2015 NAF. All rights reserved.

Page 34: curriculum.naf.orgcurriculum.naf.org/packaged/assets/downloads/technolog…  · Web viewWhen you log in to an account on a computer or a website, you enter a user ID and password

AOIT Introduction to ProgrammingLesson 8 Simple Conditional Branching and Looping

It is essential to understand this basic difference: For-loops repeat a specified number of times. While-loops continue to run while their condition is true.

While-loops are more valuable and flexible than for-loops, but programmers who aren’t careful with initializers and incrementers may create infinite loops, which will continue to run unless they are stopped manually with Ctrl+C.

The last program in this lesson, Guess-A-Number, uses a while-loop.

Copyright © 2009–2015 NAF. All rights reserved.

Page 35: curriculum.naf.orgcurriculum.naf.org/packaged/assets/downloads/technolog…  · Web viewWhen you log in to an account on a computer or a website, you enter a user ID and password

AOIT Introduction to ProgrammingLesson 8 Simple Conditional Branching and Looping

Student Resource 8.7

Notes and Practice: Looping Using While-LoopsStudent Names:_____________________________________________________ Date:____________

Directions: While you are watching the “Looping Using While-Loops” presentation, take notes and answer the questions in Part 1, below. Then do the exercises in Part 2 in the Python shell, and record the results and answer the questions in the space provided.

Part 1: Notes and Analysis1. What does a while-loop do?

2. Write a simple example of a while-loop in the space below.

3. Which code in a program gets executed when the condition of a while-loop is true?

4. Which code in a program gets executed when the condition of a while-loop is false?

5. Here is the program in slide 5 of the presentation:

# print out 10 numbers

a = 0; b = 10

while a < b:

print(a),

a = a + 1

What are the initializers?

What is the incrementer?

What number gets printed first?

What number gets printed last?

Copyright © 2009–2015 NAF. All rights reserved.

Page 36: curriculum.naf.orgcurriculum.naf.org/packaged/assets/downloads/technolog…  · Web viewWhen you log in to an account on a computer or a website, you enter a user ID and password

AOIT Introduction to ProgrammingLesson 8 Simple Conditional Branching and Looping

6. In the space below, write a program similar to the one in question 5 above. Initialize the value of a to 10 and the value of b to 0. Change the while-loop to while a is not equal to b. Decrement the value of a by 2 every time through the loop.

Answer these questions: How many times does the program go through the loop? What numbers get printed?

Part 2: Independent PracticeOpen Python IDLE and create a new program that adds numbers using a while-loop. At the beginning of the program, you ask the user how many numbers he or she wants to add. In the while-block, you prompt the user for the numbers to add.

Here are the requirements:

● Name your program add_numbers.py and save it in your Lesson 8 folder.

● Make the first statement the prompt to the user asking how many numbers to add. Assign that information to a variable called numbers_to_add.

● Initialize the sum to 0.

● Initialize the counter to 1.

● Write a while-loop that runs while the counter is less than or equal to the number of numbers the user intends to enter.

● In the while-block, prompt the user for the first number to add and assign it to a variable called number.

● Add the number to the existing sum (which, the first time through, is 0).

● Increment the counter by one.

● Print out the sum ("The sum is ").

Copyright © 2009–2015 NAF. All rights reserved.

Page 37: curriculum.naf.orgcurriculum.naf.org/packaged/assets/downloads/technolog…  · Web viewWhen you log in to an account on a computer or a website, you enter a user ID and password

AOIT Introduction to ProgrammingLesson 8 Simple Conditional Branching and Looping

Student Resource 8.8

Design and Coding: Guess-A-Number ProgramStudent Names:_____________________________________________________ Date:____________

Directions: Write the design for the Guess-A-Number program and then code it.

You will design and code the program in three stages. Follow the specific directions in each part below.

Part 1: Basic ProgramFollow the instructions below to design and code the basic version of the Guess-A-Number program.

Problem StatementWrite a program that plays the traditional “guess-a-number” game. The program establishes the “winning” number between 1 and 10 and then allows the user to try to guess the number.

In the basic version of the program, you “hard-code” the winning number into the program, and the user gets only one try to guess the number.

Requirements and Program Design (Algorithm)General Requirements

● Open a new window in Python IDLE.

● Create the Guess-A-Number program skeleton with the sections shown in the table below. Name the program number_guess.py and save it in your Lesson 8 folder.

Program Section Notes

################################ PROLOG SECTION# number_guess.py# Game to guess a number between 1 and 10.# (today's date goes here)# (programmer names go here)# (tester names go here)# Possible future enhancements:# Unresolved bugs:###############################

This section contains important information about the program.

Notice that everything in this section is included as a Python comment.

You may not be able to complete this section before you code or test the program. For example, you would list unresolved bugs right before you hand in the program for assessment. Better yet, you won’t have any unresolved bugs!

################################ ENVIRONMENT SETUP SECTION###############################

# code goes here

In the basic version of the program, there is nothing in this section.

In the intermediate and advanced versions, you’ll include a statement to import the Python random number routines that will be used to generate the number to guess.

Copyright © 2009–2015 NAF. All rights reserved.

Page 38: curriculum.naf.orgcurriculum.naf.org/packaged/assets/downloads/technolog…  · Web viewWhen you log in to an account on a computer or a website, you enter a user ID and password

AOIT Introduction to ProgrammingLesson 8 Simple Conditional Branching and Looping

################################ PROCESSING INITIALIZATION SECTION###############################

# code goes here

In this section (below this header) is setup information for the program.

Examples: code to set or initialize variables, and code to print general information to the user

################################ PROCESSING SECTION# Branching code:# Looping code:###############################

# code goes here

This section contains the main processing code.

Examples: branching and looping code

################################ CLEANUP, TERMINATION, and EXIT SECTION###############################

# code goes here

Here’s where you’ll put the final messages to the user.

Examples: “Congratulations, you guessed the number” or “Sorry, you ran out of tries”

Processing Initialization SectionAdd the following to your processing initialization section:

● Create a variable called the_number and set its value to 5. (This is the winning number in the basic program.)

● Initialize a variable called guess to the incorrect answer (make the incorrect answer be 0).

● Print an appropriate message to the user about what the game is and what the user is supposed to do to play the game. Use the problem statement above to help you create a good message. (Hint: Remember that you are talking directly to your user. Be sure to sound friendly and call your user “you.”)

Processing SectionThe following table contains the comments/pseudocode for the processing section. Write each code statement below each pseudocode statement. Some of the code has already been entered.

# get the user's guess and assign it to the variable guess_string

# test to be sure the input was numeric (for a model, see the New Password program)

# convert the string entered to an integer

guess = int(guess_string)

# check to see if the guess is in the possible range of numbers

Copyright © 2009–2015 NAF. All rights reserved.

Page 39: curriculum.naf.orgcurriculum.naf.org/packaged/assets/downloads/technolog…  · Web viewWhen you log in to an account on a computer or a website, you enter a user ID and password

AOIT Introduction to ProgrammingLesson 8 Simple Conditional Branching and Looping

if guess >= 1 and guess <= 10

# give the user a clue if the guess was too low

if guess < the_number:

# tell the user the guess was too low

print("You guessed too low.")

# give the user a clue if the guess was too high

# tell the user the guess was too high

else:

# tell the user the guess must be between 1 and 10

else:

# tell the user the input was not a number

Cleanup, Termination, and Exit SectionThe following table contains the comments/pseudocode for the processing section. Write each code statement below each pseudocode statement. Some of the code has already been entered.

# print the "winning" and "losing" message

# test to see if the user has guessed the number

if guess == the_number:

# print the winning message

else:

# print the losing message

print("Sorry, you didn't guess the number. Try playing the game again.")

Program ImplementationFollowing specific instructions from your teacher, code and run the basic version of Guess-A-Number.

Copyright © 2009–2015 NAF. All rights reserved.

Page 40: curriculum.naf.orgcurriculum.naf.org/packaged/assets/downloads/technolog…  · Web viewWhen you log in to an account on a computer or a website, you enter a user ID and password

AOIT Introduction to ProgrammingLesson 8 Simple Conditional Branching and Looping

Part 2: Intermediate ProgramFollow the instructions below to design and code the intermediate version of the Guess-A-Number program.

Problem StatementTo write the intermediate version of the Guess-A-Number program, you will modify some of the code you wrote for the basic version and add some new code. The user continues to get only one try to guess the number.

In the intermediate version you need to:

● Add the randint() random number function to pick the number. (In the basic version, you hard-coded the winning number.)

● Set the maximum number as a variable called number_max.

For specific instructions, see the “Requirements and Program Design (Algorithm)” section below.

Requirements and Program Design (Algorithm)Environment Setup SectionRetrieve the library that contains the randint() function by inserting the following lines of code in the environment setup section of your program:

# import random number routines

from random import *

Processing Initialization SectionIn place of the lines # pick the winning number and the_number = 5, insert the following lines:

# pick the winning number,

# a random number between 1 and 10

the_number = randint(1,number_max)

In place of the message "You have 1 try to guess a number between 1 and 10" (or whatever you wrote to instruct the user here), insert the following:

print("You have 1 try to guess a number between \

1 and " + str(number_max) + ".")

Note: The backward slash (\) is a line continuation character.

Program ImplementationFollowing specific instructions from your teacher, code and run the intermediate version of Guess-A-Number.

Copyright © 2009–2015 NAF. All rights reserved.

Page 41: curriculum.naf.orgcurriculum.naf.org/packaged/assets/downloads/technolog…  · Web viewWhen you log in to an account on a computer or a website, you enter a user ID and password

AOIT Introduction to ProgrammingLesson 8 Simple Conditional Branching and Looping

Part 3: Advanced ProgramFollow the instructions below to design and code the advanced version of the Guess-A-Number program.

Problem StatementTo write the advanced version of the Guess-A-Number program, you will modify some of the code you wrote for the basic and intermediate versions and add some new code. In the advanced version of the program, the user gets three tries to guess the number.

In the advanced version:

● You need to add a while-loop to give the user three tries to guess the right number.

● In adding the while-loop, you will need to re-indent some of the existing branching code.

● You need to reword one of the messages.

For specific instructions, see the “Requirements and Program Design (Algorithm)” section below.

Requirements and Program Design (Algorithm)Processing Initialization SectionAfter you add the while-loop (in the processing section), the user gets three tries to guess the right number. As you know from what you have already learned about while-loops, you need to initialize a counter and then increment it as you go through the loop.

You initialize the counter in this section. Below the line guess = 0, add the following lines of code:

# initialize the guess_counter

guess_counter = 0

One of the initial messages to the user will be incorrect and misleading in the advanced version of the program. What is it and how should it be changed? Write your solution below.

Processing SectionAdd a while-loop at the top of the processing section. The while-loop covers two conditions:

● The loop keeps repeating as long as the user hasn’t guessed the number.

● And the loop keeps repeating as long as the user hasn’t used up three tries.

The table below shows the loop and the beginning of the existing branching code.

# keep asking for a guess until user guesses the number or runs out of tries

while guess != the_number and guess_counter != 3:

Copyright © 2009–2015 NAF. All rights reserved.

Page 42: curriculum.naf.orgcurriculum.naf.org/packaged/assets/downloads/technolog…  · Web viewWhen you log in to an account on a computer or a website, you enter a user ID and password

AOIT Introduction to ProgrammingLesson 8 Simple Conditional Branching and Looping

# get the user input

guess_string = str(input("Enter your numeric guess: "))

# add to (increment) the guess counter

guess_counter = guess_counter + 1

# test to be sure the input was numeric

if guess_string.isdigit():

# convert the string entered to an integer

guess = int(guess_string)

Etc.

(Important: You must re-indent all your existing statements in the loop block. Move them all one indentation to the right, or your program will not run.)

Program ImplementationFollowing specific instructions from your teacher, code and run the advanced version of Guess-A-Number.

Make sure your assignment meets or exceeds the following assessment criteria:Program Package

• Contains all components: (1) Guess-A-Number design and coding worksheet (this document), (2) printout of the Guess-A-Number program, (3) Guess-A-Number testing worksheet, and (4) number_guess.py program in the Lesson 8 folder.

Design Document

• The algorithm represents a complete solution to the problem statement.

• The algorithm contains an appropriate level of detail for the target audience and contains both major and minor steps in the correct order.

• The document is neat and legible and does not contain spelling or grammatical errors.

Test Document

• The document contains a comprehensive set of test cases appropriate for the program objectives.

• The document is neat and legible and does not contain spelling or grammatical errors.

Program Printout

• The prolog contains the information specified in the design document.

Copyright © 2009–2015 NAF. All rights reserved.

Page 43: curriculum.naf.orgcurriculum.naf.org/packaged/assets/downloads/technolog…  · Web viewWhen you log in to an account on a computer or a website, you enter a user ID and password

AOIT Introduction to ProgrammingLesson 8 Simple Conditional Branching and Looping

• The program comments accurately describe the Python statements and are detailed enough so that peer students would be able to understand the entire program.

• The comments do not contain spelling or grammatical errors.

Python Code

• The program meets the requirements stated in the design document.

• The program runs without errors and produces the intended results.

Copyright © 2009–2015 NAF. All rights reserved.

Page 44: curriculum.naf.orgcurriculum.naf.org/packaged/assets/downloads/technolog…  · Web viewWhen you log in to an account on a computer or a website, you enter a user ID and password

AOIT Introduction to ProgrammingLesson 8 Simple Conditional Branching and Looping

Student Resource 8.9

Testing: Guess-A-Number ProgramStudent Names:_____________________________________________________ Date:____________

Directions: This is the test plan for your Guess-A-Number program. Testing of your program will be done by your testing partners using this plan as a guide. Test plans are very important in the programming world. When you interview for an internship or programming job, you should tell the interviewer that you have had experience writing and implementing test plans.

This plan consists of three tables, each of which contains three columns. The first two columns need to be filled in by you, the programmer (or coder). The third column will be filled in by your testing partners as they test your program.

A lot of the information in the first and second columns has already been filled in for you. Complete the plan by doing the following:

1. Fill in any missing information in the first column of all three tables.

2. Test your own program by following the instructions in the first column and filling in the second column. (In the programming world, programmers are often expected to do their own “unit test” before giving their program to the test team.) Fix any errors you find.

3. When you are confident your program will pass all the tests, give your program to your testers.

Important: What is a heuristic test? This test plan includes some tests that can’t be done simply by entering a single piece of input. For example, you can’t tell if the error message “your guess is too low” is working correctly unless you type in multiple guesses or possibly even play the game multiple times. These are called heuristic tests, which means you have to play around with the program for a while to determine whether it is working correctly. Before you and your testers begin the testing process, you need to come to an agreement about how these tests will be conducted.

Branching Code TestsTest Coder: Expected Response Tester: Yes/No or Comment

In response to the “Enter your guess” prompt…

1. Enter: [a null string—that is, press Enter or Return]

What is being tested: Does the program flag nonnumeric input?

Error message: “Enter a numeric guess.”

Test Coder: Expected Response Tester: Yes/No or Comment

2. Enter: bWhat is being tested: Does the

Error message: “Enter a numeric guess.”

Copyright © 2009–2015 NAF. All rights reserved.

Page 45: curriculum.naf.orgcurriculum.naf.org/packaged/assets/downloads/technolog…  · Web viewWhen you log in to an account on a computer or a website, you enter a user ID and password

AOIT Introduction to ProgrammingLesson 8 Simple Conditional Branching and Looping

program flag nonnumeric input?

3. Enter: $What is being tested: Does the program flag nonnumeric input?

Error message: “Enter a numeric guess.”

4. Enter: 0What is being tested: Does the program flag input outside the range?

Error message: “Sorry, you must enter a number between 1 and 10.”

5. Enter:

What is being tested: Does the program flag input outside the range?

Error message: “Sorry, you must enter a number between 1 and 10.”

Heuristic tests (see the note in the directions at the beginning of this worksheet)

6. Test whether the program responds correctly to a low guess.

Error message: “Sorry, you guessed too low.”

7. Test whether the program responds correctly to a high guess.

Error message: “Sorry, you guessed too high.”

8. Test whether the program responds correctly to a correct guess.

Message: “Congratulations, you guessed the number.”

Copyright © 2009–2015 NAF. All rights reserved.

Page 46: curriculum.naf.orgcurriculum.naf.org/packaged/assets/downloads/technolog…  · Web viewWhen you log in to an account on a computer or a website, you enter a user ID and password

AOIT Introduction to ProgrammingLesson 8 Simple Conditional Branching and Looping

Looping Code TestsTest Coder: Expected Response Tester: Yes/No or Comment

In response to the “Enter your guess” prompt…

9. Enter:

(any guess at the first prompt)

What is being tested: Does the program go through the loop a second time?

If the guess was not correct, program prompts a second time.

10. Enter:

What is being tested: Does the program go through the loop a third time?

11. Enter:

What is being tested: Does the loop terminate after the third time?

Message:

Environmental Tests

Test Coder: Yes/No

Tester: Yes/No or Comment

Program Execution

12. The program executes without runtime errors.

Initial Messages

13. As the program begins to execute, it prints a clear informational message explaining what is happening and what the user needs to do.

14. The initial message does not contain spelling or grammatical errors.

Final Messages

15. Before the program exits, it prints a clear message telling the user whether the guess was correct.

16. The final messages contain no spelling or grammatical errors.

Copyright © 2009–2015 NAF. All rights reserved.

Page 47: curriculum.naf.orgcurriculum.naf.org/packaged/assets/downloads/technolog…  · Web viewWhen you log in to an account on a computer or a website, you enter a user ID and password

AOIT Introduction to ProgrammingLesson 8 Simple Conditional Branching and Looping

Copyright © 2009–2015 NAF. All rights reserved.