Python Programming Practice-Flag variables

Created with Sketch.

Python Programming Practice-Flag variables

A flag variable can be used to let one part of your program know when something happens in another part of the program. Here is an example that determines if a number is prime.

num = eval ( input ( ' Enter number: ' ))
flag = 0
for i in range (2,num):
if num%i==0:
flag = 1
if flag==1:
print ( ' Not prime ' )
else :print ( ' Prime ' )

 

Recall that a number is prime if it has no divisors other than 1 and itself. The way the program above works is flag starts off at 0. We then loop from 2 to num-1 . If one of those values turns out to be a divisor, then flag gets set to 1. Once the loop is finished, we check to see if the flag got set or not. If it did, we know there was a divisor, and num isn’t prime. Otherwise, the number must be prime.

 Maxes and mins

A common programming task is to find the largest or smallest value in a series of values. Here is an example where we ask the user to enter ten positive numbers and then we print the largest one.

largest = eval ( input ( ' Enter a positive number: ' )) 
for i in range (9):
num = eval ( input ( ' Enter a positive number: ' )) 
if num>largest:
largest=num
print ( ' Largest number: ' , largest)

 

 

The key here is the variable largest that keeps track of the largest number found so far. We start by setting it equal to the the user ’s first number. Then, every time we get a new number from the user, we check to see if the user ’s number is larger than the current largest value (which is stored in largest ). If it is, then we set largest equal to the user ’s number.

If, instead, we want the smallest value, the only change necessary is that > becomes < , though it would also be good to rename the variable largest to smallest .

Later on, when we get to lists, we will see a shorter way to find the largest and smallest values, but the technique above is useful to know since you may occasionally run into situations where the list way won’t do everything you need it to do.

 

 Comments

A comment is a message to someone reading your program. Comments are often used to describe what a section of code does or how it works, especially with tricky sections of code. Comments have no effect on your program.

Single-line comments For a single-line comment, use the # character.

# a slightly sneaky way to get two values at once

num1, num2 = eval ( input ( ' Enter two numbers separated by commas: ' ))

 

You can also put comments at the end of a line:

count = count + 2 # each divisor contributes two the count

 

Multi-line comments For comments that span several lines, you can use triple quotes.

Program name: Hello world

Author: Brian Heinold Date: 1/9/11

print ( ‘ Hello world ‘ )

One nice use for the triple quotes is to comment out parts of your code. Often you will want to modify your program but don’t want to delete your old code in case your changes don’t work. You could comment out the old code so that it is still there if you need it, and it will be ignored when your new program is run. Here is a simple example:

 

print( ' This line and the next are inside a comment. ' ) 
print( ' These lines will not get executed. ' )
print ( ' This line is not in a comment and it will be executed. ' )

 

Simple debugging

Here are two simple techniques for figuring out why a program is not working:

1. Use the Python shell. After your program has run, you can type in the names of your pro- gram’s variables to inspect their values and see which ones have the values you expect them to have and which don’t. You can also use the Shell to type in small sections of your program and see if they are working.

2. Add print statements to your program. You can add these at any point in your program to see what the values of your variables are. You can also add a print statement to see if a point in your code is even being reached. For instance, if you think you might have an error in a condition of an if statement, you can put a print statement into the if block to see if the condition is being triggered.

Here is an example from the part of the primes program from earlier in this chapter. We put a print statement into the for loop to see exactly when the flag variable is being set:

flag = 0
num = eval ( input ( ' Enter number: ' ))
 for i in range (2,num):
if num%i==0:
flag = 1
print (i, flag)

 

3. An empty input statement, like below, can be used to pause your program at a specific point:

input ()

Example programs

It is a valuable skill is to be able to read code. In this section we will look in depth at some simple programs and try to understand how they work.

Example 1 The following program prints Hello a random number of times between 5 and 25.

from random import randint
rand_num = randint(5,25)
 for i in range (rand_num):
print ( ' Hello ' )

 

The first line in the program is the import statement. This just needs to appear once, usually near the beginning of your program. The next line generates a random number between 5 and 25. Then, remember that to repeat something a specified number of times, we use a for loop. To repeat some- thing 50 times, we would use range (50) in our for loop. To repeat something 100 times, we would use range (100) . To repeat something a random number of times, we can use range (rand_num) , where rand_num is a variable holding a random number. Although if we want, we can skip the variable and put the randint statement directly in the range function, as shown below.

from random import randint
for i in range (randint(5,25)):
print ( ' Hello ' )

 

Leave a Reply

Your email address will not be published. Required fields are marked *