CMPT 120: Lab 3


We want to write a program that counts for the user. It should ask them where to start, end, and what it should count by:
Start at: 3
End at: 10
Count by: 2
3 5 7 9


Start at: 100
End at: 120
Count by: 5
100 105 110 115 120

Assume that the “count by” value is 1 or larger. You can keep print from ending the line by ending the statement with a comma. For example, this program will print the numbers 1 to 10 on a single line:

for i in range(1,11):
    print i,

Write this program using a for loop.
Write this program using a while loop.


This question involves finding spaces in a string. Remember that you can extract a single character from a string by indexing: s[i] for a string s and integer i. The first character in a string is s[0]. Write a program that reports every occurrence of a space:
Enter a string: This is the user's input
There's a space in position 4.
There's a space in position 7.
There's a space in position 11.
There's a space in position 18.
Write a program that reports the position of the first space:
Enter a string: This is the user's input
The first space is in position 4.
Only test the program with strings that contain spaces. Extend the above program with the added requirement that if the string does not contain any spaces, the program prints a special message:
Enter a string: No-strings-here.
There are no spaces in this string.

Enter a string:
There are no spaces in this string.

Hints: Use a for loop for some parts, and a while loop for some others. The expression range(len (s)) will give the positions of the characters in a string s; you can use this in a for loop to iterate over each character in a string.



Chris Schmidt, last updated May 29, 2007