Difference between revisions of "CSC111 Homework 6 Solution 2014"
(→Program 2) |
|||
(6 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
--[[User:Thiebaut|D. Thiebaut]] ([[User talk:Thiebaut|talk]]) 09:46, 26 March 2014 (EDT) | --[[User:Thiebaut|D. Thiebaut]] ([[User talk:Thiebaut|talk]]) 09:46, 26 March 2014 (EDT) | ||
---- | ---- | ||
− | There were several good solution programs for [[CSC111 Homework 6 Solution 2014| Homework 6]]. You were not required to use a '''main()''' function, although some did. The | + | <onlydft> |
+ | |||
+ | There were several good solution programs for [[CSC111 Homework 6 Solution 2014| Homework 6]]. You were not required to use a '''main()''' function, although some did. The programs below were selected because they presented good features (not necessarily the same). | ||
<br /> | <br /> | ||
=Program 1= | =Program 1= | ||
+ | <br /> | ||
+ | The program below is a good example of how Python programs should be organized, with a header first, possibly a few import statements, maybe a constant or two, then a long list of function definitons ('''def''' statements), then the definition of the '''main()''' function, and at the very end, one call to '''main()''' that starts the whole program. | ||
<br /> | <br /> | ||
<source lang="python"> | <source lang="python"> | ||
Line 60: | Line 64: | ||
# ================================================================== | # ================================================================== | ||
− | # | + | # MAIN PROGRAM |
# ================================================================== | # ================================================================== | ||
# main()- set the visibility to be true and print the | # main()- set the visibility to be true and print the | ||
Line 82: | Line 86: | ||
=Program 2= | =Program 2= | ||
<br /> | <br /> | ||
+ | This program gets points for good documentation and style. | ||
<source lang="python"> | <source lang="python"> | ||
# hw6a.py | # hw6a.py | ||
Line 159: | Line 164: | ||
</source> | </source> | ||
<br /> | <br /> | ||
+ | |||
=Program 3= | =Program 3= | ||
<br /> | <br /> | ||
Line 173: | Line 179: | ||
# each square in the grid | # each square in the grid | ||
− | # | + | # Prints one row of the board. visible defines whether |
− | def printRow(visible): | + | # the row starts with white or black cell. Dim is the |
+ | # dimension of the board, controlling the # of cells on | ||
+ | # a row. | ||
+ | def printRow(visible, dim): | ||
for i in range (dim): | for i in range (dim): | ||
if visible==True: | if visible==True: | ||
Line 181: | Line 190: | ||
print("...", end="") | print("...", end="") | ||
visible = not visible | visible = not visible | ||
− | + | print() | |
− | # | + | # Prompts the user for a dimension, and then print the whole chessboard |
+ | # on the screen. | ||
def main(visibility): | def main(visibility): | ||
Line 189: | Line 199: | ||
dim = -1 | dim = -1 | ||
while dim <= -1: | while dim <= -1: | ||
− | dim = int( | + | dim = int( input("Enter dimension of board: ")) |
for i in range (dim): | for i in range (dim): | ||
− | + | printRow(visibility, dim) | |
− | + | printRow(visibility, dim) | |
− | + | printRow(visibility, dim) | |
visibility = not visibility | visibility = not visibility | ||
Line 203: | Line 213: | ||
</source> | </source> | ||
<br /> | <br /> | ||
+ | </onlydft> | ||
+ | |||
<br /> | <br /> | ||
<br /> | <br /> |