Difference between revisions of "CSC111 Programming Examples Week 2"

From dftwiki3
Jump to: navigation, search
Line 38: Line 38:
  
  
=Step 1=
+
==Step 1==
 
<source lang="python">
 
<source lang="python">
 
# Exercise1.py
 
# Exercise1.py
Line 71: Line 71:
  
 
</source>
 
</source>
=Step 2=
+
==Step 2==
 
<source lang="python">
 
<source lang="python">
 
# define constants
 
# define constants
Line 88: Line 88:
  
 
</source>
 
</source>
=Step 3=
+
==Step 3==
 
<source lang="python">
 
<source lang="python">
 
# define constants
 
# define constants
Line 109: Line 109:
 
</source>
 
</source>
  
=Step 4=
+
==Step 4==
 
<source lang="python">
 
<source lang="python">
 
# define constants
 
# define constants
Line 136: Line 136:
  
 
</source>
 
</source>
=Step 5=
+
==Step 5==
 
<source lang="python">
 
<source lang="python">
 
# define constants
 
# define constants
Line 179: Line 179:
 
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==
 
<source lang="python">
 
<source lang="python">
 
# Exercise1.py
 
# Exercise1.py
Line 235: Line 235:
  
 
</source>
 
</source>
=Step 7=
+
==Step 7==
 
<source lang="python">
 
<source lang="python">
 
# Exercise1.py
 
# Exercise1.py
Line 291: Line 291:
  
 
</source>
 
</source>
=Step 8=
+
==Step 8==
 
<br />
 
<br />
 
Activate the '''input()''' statements, again.
 
Activate the '''input()''' statements, again.

Revision as of 13:08, 6 February 2015

--D. Thiebaut (talk) 09:12, 6 February 2015 (EST)


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.

<showafterdate after="20150206 12:00" before="20150601 00:00">


Step 1

# 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

# 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

# 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

# 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 )
print( "          00...10...20...30...40...50...60...70...80...90...100" )
print( grade )
print( classAverage )

Step 5

# 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 )
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

# 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 )
print( padding + scale )
print( "grade: " + studentBar )
print( "class: " + classBar )

Step 7

# 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 )
print( padding + scale )
print( "grade: " + studentBar )
print( "class: " + classBar )

Step 8


Activate the input() statements, again.

# 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 )
print( padding + scale )
print( "grade: " + studentBar )
print( "class: " + classBar )


</showafterdate>