This site applies only to CMPT 120 D1 (Burnaby) in Summer 2011. See the other instructors' pages for other sections.
-
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 9Start at: 100
End at: 120
Count by: 5
100 105 110 115 120Assume that the “count by” value is 1 or larger and thay the end value is greater than the start value. You can keep
printfrom ending the line by ending the statement with a comma. For example, this program will print the numbers 1 to 9, counting by 2, on a single line:for i in range(1, 10, 2):
print i,- Write this program using a
forloop. - Write this program using a
whileloop.
- Write this program using a
-
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 stringsand integeri. The first character in a string iss[0].Hints: Use a
forloop for one part, and awhileloop for the other. The rangerange(len(s))will give the positions of the characters in a strings; you can use this in aforloop to iterate over each character in a string.- 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.
- Write a program that reports every occurrence of a space:
-
In a Python program, create a function named
triplethat takes one number as its argument. It should return three times that number.In the main part of the program (below the function definition), insert this code:
print "This should be nine:", triple(3)
print "This should be negative 120:", triple(-40)When this program is run, the output must be exactly this:
This should be nine: 9
This should be negative 120: -120