CSC 110
Python Coding II
Python Lab 1: if statements with else and elif
1. Write an if statement to determine if a number is divisible by 5.
Ask the user to enter a number. Determine if the number is divisible by 5.
if (number % 5 == 0):
print (str(number) + ' is divisible by 5')
2. Write an if/else statement.
An else statement can follow an if statement. The code in an else statement is executed if the expression in the if statement is false.
if (number % 5 == 0):
print (str(number) + ' is divisible by 5')
else:
print (str(number) + ' is divisible by 5')
Notice that an else statement does not have a Boolean expression.
Write an additional if/else statement to see if the number is divisible by 2.
Test your code with many different inputs.
3. Write an if/elif/else sequence.
The command elif can be used to make a series of if statements more efficient. As soon as one of the expressions is true, the other ones are skipped. elif is short for 'else if'.
Ask the user to enter a State/Province name, and print out the capital of that state. Your code should handle at least 6 different inputs. Use an if/elif/else structure
if (state == 'Wisconsin'):
print ('Madison')
elif (state == 'Colorado)':
print('Denver')
…
…
else:
print ('I do not know that one')
Test your code with many different input values.
4. Use elif in a function definition.
At a certain public pool, entrance prices are as follows:
Under 2 years: free, Age 2–11: $3, Age 11–60: $6, Over 60: $4
Complete a function that takes the age in years as input and returns the price in dollars (without the dollar sign).
def pool_admission(age):
if (age < 2):
return 0
elif (age < 12):
XXXXXXXXXXreturn 3
Notice that elif reduces possibilities and makes the code easier to read.
Python Lab 2: while loops
1. The while statement allows the commands with the body of the loop to be repeated, as long as a certain condition is true.
Ask the user to enter an integer. Store the value in x. Then add this code which counts down.
while (x > 0):
print x
x = x - 1
print ('blastoff!!')
enter an integer: 3
3
2
1
lastoff!!
2. Modify the loop to print out whether a number is even or odd. An even number is divisible by 2, and an odd number is not.
enter an integer: 3
3 is odd
2 is even
1 is odd
lastoff!!
3. Ask the user to enter in the amount of decrease. Use this value in your loop.
enter an integer: 11
enter decrease: 4
11 is odd
7 is odd
3 is odd
lastoff!!
4. Beware infinite loops! Take out the line which decreases x. What happens? Then put the line back in.
5. Write a different loop to print out words until the word has a length less than 5. The len() function returns the length of a string.
print ( word, 'has', len(word), 'letters')
enter a word: hello
hello has 5 letters
enter a word: yellow
yellow has 6 letters
enter a word: dog
goodbye
6. There are several different ways to write the code for the problem above. Discuss with your peers, or explore other possibilities on your own.
7. Write a while loop that counts from 10 to 100 in decimal, binary, and hex.
10 0b1010 0xa
11 0b1011 0x
12 0b1100 0xc
13 0b1101 0xd
14 0b1110 0xe
15 0b1111 0xf
2