This site applies only to CMPT 120 D1 (Burnaby) in Summer 2011. See the other instructors' pages for other sections.
This won't be completed in the regular lab time because of the midterm, but it's good practice anyway.
- Do the problems from the previous unmarked exercise.
-
This question involves finding spaces in a string.
- Write a program that reports the first three occurrence of a space, using a
whileloop: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.You can assume that there will be three spaces in the string. (i.e. If there are less than three spaces, there might be an error.)
- Modify your program from part (a) so that it doesn't give an error if there are less than three spaces in the string (but otherwise behaves the same).
Enter a string: This is the
There's a space in position 4.
There's a space in position 7.
- Write a program that reports the first three occurrence of a space, using a
-
In the Guide (Figure 4.1), a function
read_integeris defined that takes care of the common task of asking the user to enter a number and usingintto convert.It's also fairly common to want the user to enter a number in a specific range. For example, in the guessing game from assignment 1, question 4, you are asking the user to enter a number 1–100.
Create a function
read_integerthat takes three arguments: the prompt, the lower bound, and the upper bound on allowable numbers. The function should ask the user to enter values, until they enter one in the given range.For example, calling
read_integer("Enter your guess", 1, 100)would produce this:Enter your guess (1-100): 102
Must be in the range 1-100
Enter your guess (1-100): 0
Must be in the range 1-100
Enter your guess (1-100): 12
Then, the function should return 12.