Difference between revisions of "CSC111 Programming Examples Week 2"
(→Step 8) |
|||
(11 intermediate revisions by the same user not shown) | |||
Line 11: | Line 11: | ||
:* Also known is class average, as a number (0-100). | :* Also known is class average, as a number (0-100). | ||
:* Display student information in a box, and horizontal bar graph of 2 grades. | :* Display student information in a box, and horizontal bar graph of 2 grades. | ||
− | + | ||
− | + | ||
− | + | ;Example of Interaction with the User | |
<br /> | <br /> | ||
<source lang="text"> | <source lang="text"> | ||
Line 30: | Line 30: | ||
(The class average is set by the program, internally. The value used here in 80.) | (The class average is set by the program, internally. The value used here in 80.) | ||
<br /> | <br /> | ||
− | + | </tanbox> | |
+ | <br /> | ||
=Decomposition= | =Decomposition= | ||
Line 39: | Line 40: | ||
==Step 1== | ==Step 1== | ||
+ | <br /> | ||
+ | Here I try to get the information from the user and display it back. I also spend the time incorporating a header, and various comments that will lay down the outline of where the code will eventually go. | ||
<source lang="python"> | <source lang="python"> | ||
# Exercise1.py | # Exercise1.py | ||
Line 71: | Line 74: | ||
</source> | </source> | ||
+ | <br /> | ||
+ | |||
==Step 2== | ==Step 2== | ||
+ | <br /> | ||
+ | After verifying that the program works and gets the information well, I realize that I will spend a lot of time entering a the user information as I am debugging the program. So I right away modify the program, so that when I am debugging, I don't have to enter anything. I simply comment out the input statements by putting a #-sign in front of them. This way they actually ''disappear'' from the program for the interpreter. Instead I assign constant strings and numbers to the variables. | ||
+ | <br /> | ||
+ | <br /> | ||
<source lang="python"> | <source lang="python"> | ||
+ | # (header removed) | ||
# define constants | # define constants | ||
classAverage = 70 | classAverage = 70 | ||
Line 88: | Line 98: | ||
</source> | </source> | ||
+ | |||
+ | <br /> | ||
+ | <br /> | ||
==Step 3== | ==Step 3== | ||
+ | <br /> | ||
+ | Next I copy/paste the top and bottom bars of the box and make them strings by putting double-quotes around them. | ||
+ | <br /> | ||
+ | I also print the scale. | ||
+ | <br /> | ||
<source lang="python"> | <source lang="python"> | ||
# define constants | # define constants | ||
Line 108: | Line 126: | ||
</source> | </source> | ||
+ | |||
+ | <br /> | ||
==Step 4== | ==Step 4== | ||
+ | <br /> | ||
+ | Now we start to have some more sophisticated code to come up with. We need to space out the first name, last name and Id in the box, and figure out the right number of spaces between them. I use as many variables as make sense for this problem. I try to give them names that will help somebody reading the code understand what quantities they contain. | ||
+ | <br /> | ||
+ | |||
<source lang="python"> | <source lang="python"> | ||
# define constants | # define constants | ||
Line 138: | Line 162: | ||
</source> | </source> | ||
+ | |||
+ | <br /> | ||
+ | <br /> | ||
==Step 5== | ==Step 5== | ||
+ | <br /> | ||
+ | Now comes the part where I need to figure out how to display a bar of #-signs, whose length is proportional to the grade. Looking at the scale, i see that we use 5 characters for an interval of 10. Between 10 and 20, there's 5 characters: 2 digits (10) and 3 dots (...). So if the student's grade is 10, I need to print 5 #-signs. If the grade is 20, I need to print 10 #-signs. The relationship I'm after is thus: number of #-signs = grade divided by 2. | ||
+ | <br /> | ||
+ | I add two sections to my code, that look identical, and that allow me to print two bars, for the student grade and for the class average. | ||
+ | <br /> | ||
+ | |||
<source lang="python"> | <source lang="python"> | ||
# define constants | # define constants | ||
Line 183: | Line 216: | ||
We note that the scale is not quite accurate and needs to be "pushed" a bit to the right... | We note that the scale is not quite accurate and needs to be "pushed" a bit to the right... | ||
<br /> | <br /> | ||
+ | |||
==Step 6== | ==Step 6== | ||
+ | <br /> | ||
+ | In this step, I add some "padding" of space-characters in front of the scale. | ||
+ | <br /> | ||
+ | <br /> | ||
<source lang="python"> | <source lang="python"> | ||
# Exercise1.py | # Exercise1.py | ||
Line 241: | Line 279: | ||
</source> | </source> | ||
+ | |||
+ | <br /> | ||
+ | <br /> | ||
==Step 7== | ==Step 7== | ||
+ | <br /> | ||
+ | We're getting very close to having a finished program. Here's where we are. The program still doesn't get the information from the user, but in other ways gets the job done. | ||
+ | <br /> | ||
+ | <br /> | ||
<source lang="python"> | <source lang="python"> | ||
# Exercise1.py | # Exercise1.py | ||
Line 299: | Line 344: | ||
</source> | </source> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | |||
==Step 8== | ==Step 8== | ||
<br /> | <br /> | ||
− | + | Ok, now is time to activate the '''input()''' statements, again. Once we have them back in, we have to '''TEST''' our program and make sure it works well in as many "strange" conditions as | |
+ | * an empty string for the first name and for the last name. | ||
+ | * an empty string for the Id | ||
+ | * a grade of 0 | ||
+ | * a grade of 100 | ||
+ | * a very long name | ||
+ | <br /> | ||
+ | <br > | ||
<source lang="python"> | <source lang="python"> | ||
# Exercise1.py | # Exercise1.py |
Latest revision as of 20:37, 7 February 2015
--D. Thiebaut (talk) 09:12, 6 February 2015 (EST)
Contents
Problem
Here's a problem, below. Read it. And then figure out how you could write a short program that would just be the seed for a solution. Then try to write that small program. And then, in a series of successive small step, you get closer and closer to the solution. I have shown at the bottom of the page my own progression, going from a very simple seed to a fool blown solution. The trick is to do add something fairly simple at every step.
- Problem
-
- Get first name, last name, Id from student, and final grade, as a number (0-100).
- Also known is class average, as a number (0-100).
- Display student information in a box, and horizontal bar graph of 2 grades.
- Example of Interaction with the User
First name? Dominique
Last name? Thiebaut
Id? 990123456
Final grade? 90
+———————————————————————————————————-———————————-+
|Dominique Thiebaut 990123456 |
+———————————————————————————————————-———————————-+
00...10...20...30...40...50...60...70...80...90...100
grade: #############################################
class: ########################################
(The class average is set by the program, internally. The value used here in 80.)
Decomposition
Below are various steps showing how to progress through the writing of a solution program.
Step 1
Here I try to get the information from the user and display it back. I also spend the time incorporating a header, and various comments that will lay down the outline of where the code will eventually go.
# Exercise1.py
# D. Thiebaut
# 2/6/15
# This program prompts the user for some student information
# including name, Id, and final grade, and displays the result
# nicely formatted.
# Example of output:
#
#+———————————————————————————————————-———————————-+
#|Alex Andra 990123456 |
#+———————————————————————————————————————————-———-+
# 00...10...20...30...40...50...60...70...80...90...100
#grade: #########################
#class: #####################
#
# define constants
classAverage = 70
# get user data
fName = input( "First name? " )
lName = input( "Last name? " )
Id = input( "Id? " )
grade = eval( input( "Final grade? " ) )
# display result
print( fName, lName, Id )
print( grade )
print( classAverage )
Step 2
After verifying that the program works and gets the information well, I realize that I will spend a lot of time entering a the user information as I am debugging the program. So I right away modify the program, so that when I am debugging, I don't have to enter anything. I simply comment out the input statements by putting a #-sign in front of them. This way they actually disappear from the program for the interpreter. Instead I assign constant strings and numbers to the variables.
# (header removed)
# define constants
classAverage = 70
# get user data
fName = "Alex" #input( "First name? " )
lName = "Andra" #input( "Last name? " )
Id = "990123456" #input( "Id? " )
grade = 80 #eval( input( "Final grade? " ) )
# display result
print( fName, lName, Id )
print( grade )
print( classAverage )
Step 3
Next I copy/paste the top and bottom bars of the box and make them strings by putting double-quotes around them.
I also print the scale.
# define constants
classAverage = 70
# get user data
fName = "Alex" #input( "First name? " )
lName = "Andra" #input( "Last name? " )
Id = "990123456" #input( "Id? " )
grade = 80 #eval( input( "Final grade? " ) )
# display result
print( "+———————————————————————————————————-———————————-+" )
print( fName, lName, Id )
print( "+———————————————————————————————————-———————————-+" )
print( " 00...10...20...30...40...50...60...70...80...90...100" )
print( grade )
print( classAverage )
Step 4
Now we start to have some more sophisticated code to come up with. We need to space out the first name, last name and Id in the box, and figure out the right number of spaces between them. I use as many variables as make sense for this problem. I try to give them names that will help somebody reading the code understand what quantities they contain.
# define constants
classAverage = 70
# get user data
fName = "Alex" #input( "First name? " )
lName = "Andra" #input( "Last name? " )
Id = "990123456" #input( "Id? " )
grade = 80 #eval( input( "Final grade? " ) )
# create header
bar = "+———————————————————————————————————-———————————-+"
barLen = len( bar )
userLen = len( "|" + fName+ " " + lName + Id + " |" )
noSpaces = barLen - userLen
infoLine = "|" + fName + " " + lName + (" "*noSpaces) + Id + " |"
# display header
print( bar )
print( infoLine )
print( bar )
# display bar graph
print( " 00...10...20...30...40...50...60...70...80...90...100" )
print( grade )
print( classAverage )
Step 5
Now comes the part where I need to figure out how to display a bar of #-signs, whose length is proportional to the grade. Looking at the scale, i see that we use 5 characters for an interval of 10. Between 10 and 20, there's 5 characters: 2 digits (10) and 3 dots (...). So if the student's grade is 10, I need to print 5 #-signs. If the grade is 20, I need to print 10 #-signs. The relationship I'm after is thus: number of #-signs = grade divided by 2.
I add two sections to my code, that look identical, and that allow me to print two bars, for the student grade and for the class average.
# define constants
classAverage = 100
# get user data
fName = "Alex" #input( "First name? " )
lName = "Andra" #input( "Last name? " )
Id = "990123456" #input( "Id? " )
grade = 80 #eval( input( "Final grade? " ) )
# create header
bar = "+———————————————————————————————————-———————————-+"
barLen = len( bar )
userLen = len( "|" + fName+ " " + lName + Id + " |" )
noSpaces = barLen - userLen
infoLine = "|" + fName + " " + lName + (" "*noSpaces) + Id + " |"
# create bar graph
padding = " "
scale = "00...10...20...30...40...50...60...70...80...90...100"
# create user bar
noHashTags = int( grade/2 )
studentBar = noHashTags * '#'
# create class bar
noHashTags = int( classAverage/2 )
classBar = noHashTags * '#'
# display header
print( bar )
print( infoLine )
print( bar )
# display bar graph
print( padding + scale )
print( "grade: " + studentBar )
print( "class: " + classBar )
We note that the scale is not quite accurate and needs to be "pushed" a bit to the right...
Step 6
In this step, I add some "padding" of space-characters in front of the scale.
# Exercise1.py
# D. Thiebaut
# 2/6/15
# This program prompts the user for some student information
# including name, Id, and final grade, and displays the result
# nicely formatted.
# Example of output:
#
#+———————————————————————————————————-———————————-+
#|Alex Andra 990123456 |
#+———————————————————————————————————————————-———-+
# 00...10...20...40...50...60...70...80...90...100
#grade: #######################################
#class: ##################################
#
# define constants
classAverage = 80
# get user data
fName = "Alex" #input( "First name? " )
lName = "Andra" #input( "Last name? " )
Id = "990123456" #input( "Id? " )
grade = 70 #eval( input( "Final grade? " ) )
# create header
bar = "+———————————————————————————————————-———————————-+"
barLen = len( bar )
userLen = len( "|" + fName+ " " + lName + Id + " |" )
noSpaces = barLen - userLen
infoLine = "|" + fName + " " + lName + (" "*noSpaces) + Id + " |"
# create bar graph
padding = " "
scale = "00...10...20...30...40...50...60...70...80...90...100"
# create user bar
noHashTags = int( grade/2 )
studentBar = noHashTags * '#'
# create class bar
noHashTags = int( classAverage/2 )
classBar = noHashTags * '#'
# display header
print( bar )
print( infoLine )
print( bar )
# display bar graph
print( padding + scale )
print( "grade: " + studentBar )
print( "class: " + classBar )
Step 7
We're getting very close to having a finished program. Here's where we are. The program still doesn't get the information from the user, but in other ways gets the job done.
# Exercise1.py
# D. Thiebaut
# 2/6/15
# This program prompts the user for some student information
# including name, Id, and final grade, and displays the result
# nicely formatted.
# Example of output:
#
#+———————————————————————————————————-———————————-+
#|Alex Andra 990123456 |
#+———————————————————————————————————————————-———-+
# 00...10...20...40...50...60...70...80...90...100
#grade: #######################################
#class: ##################################
#
# define constants
classAverage = 80
# get user data
fName = "Alex" #input( "First name? " )
lName = "Andra" #input( "Last name? " )
Id = "990123456" #input( "Id? " )
grade = 70 #eval( input( "Final grade? " ) )
# create header
bar = "+———————————————————————————————————-———————————-+"
barLen = len( bar )
userLen = len( "|" + fName+ " " + lName + Id + " |" )
noSpaces = barLen - userLen
infoLine = "|" + fName + " " + lName + (" "*noSpaces) + Id + " |"
# create bar graph
padding = " "
scale = "00...10...20...30...40...50...60...70...80...90...100"
# create user bar
noHashTags = int( grade/2 )
studentBar = noHashTags * '#'
# create class bar
noHashTags = int( classAverage/2 )
classBar = noHashTags * '#'
# display header
print( bar )
print( infoLine )
print( bar )
# display bar graph
print( padding + scale )
print( "grade: " + studentBar )
print( "class: " + classBar )
Step 8
Ok, now is time to activate the input() statements, again. Once we have them back in, we have to TEST our program and make sure it works well in as many "strange" conditions as
- an empty string for the first name and for the last name.
- an empty string for the Id
- a grade of 0
- a grade of 100
- a very long name
# Exercise1.py
# D. Thiebaut
# 2/6/15
# This program prompts the user for some student information
# including name, Id, and final grade, and displays the result
# nicely formatted.
# Example of output:
#
#+———————————————————————————————————-———————————-+
#|Alex Andra 990123456 |
#+———————————————————————————————————————————-———-+
# 00...10...20...40...50...60...70...80...90...100
#grade: #######################################
#class: ##################################
#
# define constants
classAverage = 80
# get user data
fName = input( "First name? " )
lName = input( "Last name? " )
Id = input( "Id? " )
grade = eval( input( "Final grade? " ) )
# create header
bar = "+———————————————————————————————————-———————————-+"
barLen = len( bar )
userLen = len( "|" + fName+ " " + lName + Id + " |" )
noSpaces = barLen - userLen
infoLine = "|" + fName + " " + lName + (" "*noSpaces) + Id + " |"
# create bar graph
padding = " "
scale = "00...10...20...30...40...50...60...70...80...90...100"
# create user bar
noHashTags = int( grade/2 )
studentBar = noHashTags * '#'
# create class bar
noHashTags = int( classAverage/2 )
classBar = noHashTags * '#'
# display header
print( bar )
print( infoLine )
print( bar )
# display bar graph
print( padding + scale )
print( "grade: " + studentBar )
print( "class: " + classBar )