Difference between revisions of "CSC111 Lab 2 2014"

From dftwiki3
Jump to: navigation, search
(Functions: abs(), int(), str())
Line 129: Line 129:
 
Below is a series of statements you should type in the console, and as you see how Python interprets these statements, this should help you figure out how the functions work.
 
Below is a series of statements you should type in the console, and as you see how Python interprets these statements, this should help you figure out how the functions work.
  
<tanbox>Note: some of the statements listed below will result in errors.  You should figure out why the error is generated.  Also, before you press enter  
+
<tanbox>Note: some of the statements listed below will result in errors.  You should figure out why the error is generated.  Also, before you press enter, try to predict what Python will output.
 +
</tanbox>
 +
 
 
<code><pre>
 
<code><pre>
 
>>> a = 10.34
 
>>> a = 10.34

Revision as of 16:15, 4 February 2014

--D. Thiebaut (talk) 14:06, 4 February 2014 (EST)



Teller Machine Program

ATM.jpg


This section has to do with a program that takes some 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.

Pseudo-Code


On a piece of paper, figure out an algorithm for taking the amount of money to withdraw from the ATM, or teller machine, and compute the number of different bills. Write it down in pseudocode, i.e. a mixture of English and Python.

  • You need to start with some amount
  • You compute first the number of $20 bills,
  • then the number of $10 bills,
  • ...
  • down to the number of $1 bills.


Comments


  • Use Idle and open a new window.
  • In a new window, put down your pseudo code in several lines and put a #-sign in front of each line, so that they become Python comments.
  • Try to run the program and verify that no errors are detected. There shouldn't be any Python code in your program, only comments. Idle will be able to tell you if it sees anything else that is not a comment!


Adding Code


  • Now that you have the structure for your program, add enough Python code to make your code display
    • The total amount
    • The number of $20-bills


  • When you get the right number output, add some more python to get the program to print the amount of $10-bills.


  • Finish up the program


  • Verify that your program works. Below is a typical output you should try to emulate:


Amount to withdraw =  97

Please lift keyboard and find: 
4 $20-bill(s)
1 $10-bill(s)
1 $5-bill(s)
2 $1-bill(s)


Using Constants

  • You probably have statements like these in your program


 no20 = amount // '''20'''
 leftover = amount % '''20'''


Instead of using the literals 20, 10, 5, and 1 in the program, we can use variables to hold them. It adds to the complexity a tiny bit, but makes the program more useful. You'll see how.


  • Modify your program and add these variables near the top of the program:


DENOM1 = 20
DENOM2 = 10
DENOM3 = 5
DENOM4 = 1


  • and where you compute the number of $20-bills, replace 20 by DENOM1. You should get something like this:


noBills = amount // DENOM1
leftover = amount % DENOM1
print( noBills, "$", DENOM1, "bill(s)" )


  • Run your program. Make sure it still outputs the correct result after this modification. If not, fix the error(s)!
  • Replace 10, 5, and 1 by DENOM2, DENOM3, DENOM4 in the remaining part of your program.


  • Run your program and make sure it sill outputs the correct result.


Flexibility and Adaptability


Imagine that your program will be used in an area where the bills do not come in 20, 10, 5 or 1 denominations, but in 100, 50, 10, and 1.

Figure out a way to make the least amount of change to your program so that it now outputs the correct break down for any amount, but in $100-, $50-, $10- and $1-bills.

Make sure your program works! Below is the output of the program if the amount is set to $97:

Amount to withdraw = $ 97

Please lift keyboard and find: 
0 $ 100 bill(s)
1 $ 50 bill(s)
4 $ 10 bill(s)
7 $ 1 bill(s)

Challenge #1: A Change Machine

QuestionMark1.jpg


  • Using a similar approach, write a program that, given some number of pennies, will output the correct number of quarters, dimes, nickels, and pennies. You should initialize the amount of pennies with an integer, like this:


 pennies = 84


  • Here is an example of what your program should output for 84 pennies:
USCoins.jpg
Amount to withdraw = 84 penny or pennies

Please lift keyboard and find: 
3 quarter(s)
0 dime(s)
1 nickel(s)
4 penny or pennies


Functions: abs(), int(), str()


Python supports many functions. A very good way to understand how they work is to try them in the Console window of Idle, i.e. the window where programs output their result.
Below is a series of statements you should type in the console, and as you see how Python interprets these statements, this should help you figure out how the functions work.

Note: some of the statements listed below will result in errors. You should figure out why the error is generated. Also, before you press enter, try to predict what Python will output.
>>> a = 10.34
>>> a

>>> int( a )

>>> a = -10.34
>>> abs( a )

>>> abs( a * 100 )

>>> str( 10.34 )

>>> 'hello' + 10.34

>>> 'hello' + str( 10.34 )

>>> amount = 123.45
>>> int( amount )

>>> int( amount * 10 )

>>> int( amount / 10 )

>>> amount  = 1234
>>> 1234 // 10

>>> 'a' * '5'

>>> 'a' * 5

>>> '3' * 5

>>> 3 * 5

>>> str( 3 ) + str( 5 )

>>> str( 3 ) * 5

</code>