Difference between revisions of "CSC111 Lab 3 2018"
(→Challenge #0: (we always start at 0 in Computer Science!)) |
|||
(6 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
[[User:Thiebaut|D. Thiebaut]] ([[User talk:Thiebaut|talk]]) 16:27, 11 February 2018 (EST) | [[User:Thiebaut|D. Thiebaut]] ([[User talk:Thiebaut|talk]]) 16:27, 11 February 2018 (EST) | ||
---- | ---- | ||
− | + | <onlydft> | |
Line 111: | Line 111: | ||
[[Image:ATM.jpg|right|200px]] | [[Image:ATM.jpg|right|200px]] | ||
<br /> | <br /> | ||
− | This section will get you to write a program similar (though not necessarily the same) to the program we wrote in class | + | This section will get you to write a program similar (though not necessarily the same) to the program we wrote in class. The program takes an ''integer'' (without a decimal part) amount of dollars and figures out how to break it down into the least number of $20-, $10-, $5-, and $1-bills. |
<br /> | <br /> | ||
<br /> | <br /> | ||
Line 232: | Line 232: | ||
<br /> | <br /> | ||
− | * Using a similar approach, write a program that | + | * Using a similar approach, write a program that prompts the user for a number of pennies, and that outputs the correct number of quarters, dimes, nickels, and pennies. You should ''initialize'' the amount of pennies like this: |
<br /> | <br /> | ||
::<source lang="python"> | ::<source lang="python"> | ||
− | pennies = | + | pennies = eval( input( "Number of pennies? " ) ) |
</source> | </source> | ||
<br /> | <br /> | ||
Line 243: | Line 243: | ||
[[Image:USCoins.jpg|right|200px]] | [[Image:USCoins.jpg|right|200px]] | ||
− | + | Number of pennies? 84 | |
+ | |||
Please lift keyboard and find: | Please lift keyboard and find: | ||
3 quarter(s) | 3 quarter(s) | ||
Line 287: | Line 288: | ||
* Then try these different formats: {0:>1}, {0:>5}, {0:>10}, {0:>15}, {0:>20}. Notice the right-justification of the string Snow White in this second set of outputs. | * Then try these different formats: {0:>1}, {0:>5}, {0:>10}, {0:>15}, {0:>20}. Notice the right-justification of the string Snow White in this second set of outputs. | ||
<br /> | <br /> | ||
− | * Now try this | + | * Now try this section of code: |
<br /> | <br /> | ||
::<source lang="python"> | ::<source lang="python"> | ||
Line 464: | Line 465: | ||
==Sum of a list of numbers== | ==Sum of a list of numbers== | ||
<br /> | <br /> | ||
− | * Add this | + | * Add this section of code to your program (or create a new program): |
<br /> | <br /> | ||
::<source lang="python"> | ::<source lang="python"> | ||
Line 510: | Line 511: | ||
==Count the Items in a List== | ==Count the Items in a List== | ||
<br /> | <br /> | ||
− | * Try this | + | * Try this section of code now: |
<br /> | <br /> | ||
::<source lang="python"> | ::<source lang="python"> | ||
Line 560: | Line 561: | ||
<br /> | <br /> | ||
− | <br /> | + | <!-- br /> |
{| style="width:100%; background:silver" | {| style="width:100%; background:silver" | ||
|- | |- | ||
| | | | ||
− | ==Challenge #7: (Challenging!)== | + | ==Challenge #7: (Challenging and Optional!)== |
|} | |} | ||
[[Image:QuestionMark9.jpg|right|120px]] | [[Image:QuestionMark9.jpg|right|120px]] | ||
<br /> | <br /> | ||
* Write a program that contains a single loop, of the form <font color="magenta"><tt>for n in [ 2, 3, 5, 3, 2 ]:</tt></font>, and that accumulates a string, which, when it is fully computed, is equal to "**---*****---**". | * Write a program that contains a single loop, of the form <font color="magenta"><tt>for n in [ 2, 3, 5, 3, 2 ]:</tt></font>, and that accumulates a string, which, when it is fully computed, is equal to "**---*****---**". | ||
+ | * '''Hints:''' Here are a few lines of Python code to start with... | ||
<br /> | <br /> | ||
+ | ::<source lang="python"> | ||
+ | def main(): | ||
+ | result = "" | ||
+ | for n in [ 2, 3, 5, 3, 2 ]: | ||
+ | ... | ||
+ | |||
+ | print( result ) | ||
+ | |||
+ | main() | ||
+ | </source> | ||
+ | <br / --> | ||
+ | </onlydft> | ||
<br /> | <br /> | ||
− | <showafterdate after=" | + | <showafterdate after="20180218 9:00" before="20180601 00:00"> |
<br /> | <br /> | ||
<br /> | <br /> |
Latest revision as of 12:49, 1 June 2018
D. Thiebaut (talk) 16:27, 11 February 2018 (EST)
<showafterdate after="20180218 9:00" before="20180601 00:00">
Solution Program
# lab3Solutions.py # D. Thiebaut # Solution programs for CSC111 Lab #3 # # All the solutions are group into one large main program. def main(): # Challenge 0 for x in [ 0, 1, 24.99, 25, 25.001, 30, 51, 79, 99]: result = int( x/25 ) print( "x = ", x, " result = ", result ) # Teller Machine # Takes some amount of $ and breaks it down in 20, 10, 5, and 1 # dollar bills. # set the amount amount = 97 # break it down no20s = amount // 20 leftOver = amount % 20 no10s = leftOver // 10 leftOver = leftOver % 10 no5s = leftOver // 5 leftOver = leftOver % 5 no1s = leftOver # printout the number of bills print() print( "You want to withdraw ${0:1}".format( amount) ) print( "Lift the keyboard and find:" ) print( "{0:4} $20-bill(s)". format( no20s ) ) print( "{0:4} $10-bill(s)". format( no10s ) ) print( "{0:4} $5-bill(s)". format( no5s ) ) print( "{0:4} $1-bill(s)". format( no1s ) ) # ========================================================== # Teller Machine with a different set of denominations # set the amount amount = 197 # break it down no100s = amount // 100 leftOver = amount % 100 no50s = leftOver // 50 leftOver = leftOver % 50 no10s = leftOver // 10 leftOver = leftOver % 10 no1s = leftOver # printout the number of bills print() print( "You want to withdraw ${0:1}".format( amount) ) print( "Lift the keyboard and find:" ) print( "{0:4} $100-bill(s)". format( no20s ) ) print( "{0:4} $50-bill(s)". format( no10s ) ) print( "{0:4} $10-bill(s)". format( no5s ) ) print( "{0:4} $1-bill(s)". format( no1s ) ) # simple output print() name = "Snow White" print( "Hello {0:1}! How are you?".format( name ) ) print( "Hello {0:5}! How are you?".format( name ) ) print( "Hello {0:10}! How are you?".format( name ) ) print( "Hello {0:15}! How are you?".format( name ) ) print( "Hello {0:20}! How are you?".format( name ) ) print( "Hello {0:>1}! How are you?".format( name ) ) print( "Hello {0:>5}! How are you?".format( name ) ) print( "Hello {0:>10}! How are you?".format( name ) ) print( "Hello {0:>15}! How are you?".format( name ) ) print( "Hello {0:>20}! How are you?".format( name ) ) # Formatted output, Version 1 print( ) for friend in [ "Bashful", "Doc", "Dopey", "Happy", "Sleepy", "Sneezy", "Grumpy"]: print( "Hello {0:10}!".format( friend ) ) # Swapping first and second strings print( ) for friend in [ "Bashful", "Doc", "Dopey", "Happy", "Sleepy", "Sneezy", "Grumpy"]: print( "{1:1} wants to take {0:1} to the Valentine's ball." . format( friend, "Snow White" ) ) for friend in [ "Bashful", "Doc", "Dopey", "Happy", "Sleepy", "Sneezy", "Grumpy"]: print( "{0:1} wants to take {1:1} to the Valentine's ball." . format( friend, "Snow White" ) ) # Unformatted dwarf names and length of names for friend in [ "Bashful", "Doc", "Dopey", "Happy", "Sleepy", "Sneezy", "Grumpy"]: print( friend, len(friend) ) # Formatted output, Version 2 bar = "+-" + 10*'-' + "-+-" + 3*'-' + "-+" print( bar ) for friend in [ "Bashful", "Doc", "Dopey", "Happy", "Sleepy", "Sneezy", "Grumpy"]: print( "| {0:10} | {1:3} |".format( friend, len(friend) ) ) print( bar ) # print string and int name = "Valentine's Day" day = 15 print( "{0:>20} is on the {1:1}th of the month" . format( name, day ) ) name = "My birthday" day = 6 print( "{0:>20} is on the {1:1}th of the month" . format( name, day ) ) # printing floats print() print( "Printing PI" ) pi = 3.14159 print( "Pi={0:1.0f}!".format( pi ) ) print( "Pi={0:2.0f}!".format( pi ) ) print( "Pi={0:3.0f}!".format( pi ) ) print( "Pi={0:4.0f}!".format( pi ) ) print( "Pi={0:5.0f}!".format( pi ) ) print( "Pi={0:6.0f}!".format( pi ) ) print( "Pi={0:7.0f}!".format( pi ) ) print( "Pi={0:8.0f}!".format( pi ) ) print( "Pi={0:9.0f}!".format( pi ) ) print( "-------------------------" ) print( "Pi={0:9.1f}!".format( pi ) ) print( "Pi={0:9.2f}!".format( pi ) ) print( "Pi={0:9.3f}!".format( pi ) ) print( "Pi={0:9.4f}!".format( pi ) ) print( "Pi={0:9.5f}!".format( pi ) ) print( "-------------------------" ) print( "Pi={0:<9.1f}!".format( pi ) ) print( "Pi={0:<9.2f}!".format( pi ) ) print( "Pi={0:<9.3f}!".format( pi ) ) print( "Pi={0:<9.4f}!".format( pi ) ) print( "Pi={0:<9.5f}!".format( pi ) ) # Print table of temperatures print() for Fahr in range( 20, 41, 2 ): print( Fahr, (Fahr-32.0)*5.0/9 ) print() for Fahr in range( 20, 41, 2 ): print( "{0:10.2f}F = {1:10.2f}C".format( Fahr*1.0, (Fahr-32.0)*5.0/9 ) ) # Print table of temperatures with bars around bar = "+-" + 10*'-' + "--+-" + 10*'-' + "--+" print( bar ) print( "| {0:>10} | {1:>10} |".format( "Fahrenheit", "Celsius" ) ) print( bar ) for Fahr in range( 20, 41, 2 ): print( "| {0:10.2f}F | {1:10.2f}C |".format( Fahr*1.0, (Fahr-32.0)*5.0/9 ) ) print( bar ) def main2(): # compute the sum a list of numbers # (note that there's a much simpler way to do this in Python, which # we'll see later during the semester.) print() sumAll = 0 for n in [100, 10, -30, -2, 20, 5 ]: sumAll = sumAll + n print( "n = {0:3} sum = {1:3}" . format( n, sumAll ) ) print( "sum = ", sumAll ) # add-up the even numbers between 0 and 100. print() sumAll = 0 for n in range( 0, 101, 2 ): sumAll = sumAll + n print( "the sum of all the even numbers from 0 to 100, included, is", sumAll ) # count how many items are in the list print() count = 0 for n in [ 100, 10, -30, -2, 20, 5 ]: count = count + 1 print( "n = {0:3} count = {1:3}" . format( n, count ) ) print( "there are", count, "items in the list" ) # compute the sum and average of a list of numbers print() sumAll = 0 count = 0 for n in [1, 2, 2, 1, 2, 3, 3, 2, 1, 0]: sumAll = sumAll + n count = count + 1 print( "sum = {0:1} average = {1:1.2f}" . format( sumAll, sumAll*1.0/count ) ) # compute the factorial of a number # factorial( 10 ) = 10 * 9 * 8 * 7 * 6 * 5 * 4 * 3 * 2 * 1 print() n = 20 fact = 1 for i in range( 1, n+1 ): print( "computing fact * i = {0:1} * {1:1} = {2:1}" .format( fact, i, fact*i ) ) fact = fact * i print( "factorial({0:1}) = {1:1}".format( n, fact ) ) # create a string with all the names of the 7 dwarves print() line = "" for friend in [ "Bashful", "Doc", "Dopey", "Happy", "Sleepy", "Sneezy", "Grumpy"]: line = line + friend + ", " print( "line =", line ) # Challenge print() line = "" char1 = "*" char2 = "-" for n in ( 2, 3, 5, 3, 2 ): line = line + n*char1 char1, char2 = char2, char1 print( "line = ", line ) main2()
</showafterdate>