Python Programming: An Introduction to Computer Science

(Nora) #1

Chapter 3


Computing with Numbers


Whencomputerswerefirstdeveloped,they wereseenprimarilyasnumbercrunchers,andthatisstillan
importantapplication.Asyouhave seen,problemsthatinvolve mathematicalformulasareeasytotranslate
intoPythonprograms.Thischaptertakesa closerlookat computationsinvolvingnumericcalculations.


3.1 NumericDataTypes


Theinformationthatisstoredandmanipulatedbycomputerprogramsisgenericallyreferredtoasdata.
Differentkindsofdatawillbestoredandmanipulatedin differentways.Considerthisprogramthatcalculates
thevalueofloosechange.


change.py


A program to calculatethe value of some change in dollars


def main():
print "Change Counter"
print
print "Please enterthe count of each coin type."
quarters = input("Quarters:")
dimes = input("Dimes:")
nickels = input("Nickels:")
pennies = input("Pennies:")
total = quarters .25 + dimes .10 + nickels .05 + pennies .01
print
print "The totalvalue of your change is", total


main()


Hereis anexampleoftheoutput.


Change Counter


Please enter the countof each coin type.
Quarters: 5
Dimes: 3
Nickels: 4
Pennies: 6


The total value of yourchange is 1.81


Thisprogramactuallymanipulatestwo differentkindsofnumbers.Thevaluesenteredbytheuser(5,3,
4,6)arearewholenumbers;they don’t have any fractionalpart.Thevaluesofthecoins(.25,.10,.05,.01)


25
Free download pdf