Contents
- binaryToDecimal.py
- flow_example.py
- lab2.4.py
- lab2.4.withfunc.py
- menu.py
- menu_withfunctions.py
- menu_withoutfunc.py
- oldmac.py
binaryToDecimal.py 1/8
[top][prev][next]
# Converts a binary number into a decimal
# Modify to verify that the inputted number is valid.
# By CSCI111, 02.16.2011
# Refactored by CSCI111, 02.28.2011
import sys
def main():
# Read in the binary number as a string -- why?
num = raw_input("Enter the binary #: ")
# --------- Validate the user input ----------
while not binaryTest(num):
print num, "is not a valid binary number. Please try again."
num = raw_input("Enter the binary #: ")
decVal = binaryToDecimal(num)
print "The decimal value for", num, "is", decVal
# Converts the binary number to a decimal number
# Precondition: binary, a string that is a binary number
# Postcondition: returns the decimal value of the binary number
def binaryToDecimal(binary):
# accumulate the decimal value in this variable
decVal = 0
# go through the positions in the string
for pos in xrange(len(binary)):
# num[pos] is a string; need to convert to an int
bit = int(binary[pos])
# calculate which "place" the current bit is at
place = 2**(len(binary)-pos-1)
# add to the decimal value
decVal += place * bit
return decVal
# precondition: num is a string
# postcondition: returns True iff num is a valid binary string
def binaryTest(num):
# check that it has all digits (no letters)
if not num.isdigit():
return False
# Make sure that the inputted number only contains 0s and 1s
for digit in num:
if digit != "0" and digit != "1":
return False
return True
main()
flow_example.py 2/8
[top][prev][next]
def max(num1, num2):
result = 0
if num1 >= num2:
result = num1
else:
result = num2
return result
x = 2
y = input("Enter a number: ")
z = max(x, y)
print "The max is", z
lab2.4.py 3/8
[top][prev][next]
# lab2.4.py
#
# Displays equivalents for Olympic running distances in
# meters, kilometers, yards, miles
# conversion constants
M2KILO=.001
M2YARD=1.094
M2MILE=.0006215
# distances in meters
dist1=100
dist2=200
dist3=400
dist4=800
# print column headings
print "Meters Kilometers Yards Miles"
print "-"*32
# calculate conversions, print data for conversion table
kilo1=dist1*M2KILO
yard1=dist1*M2YARD
mile1=dist1*M2MILE
print "%6.0i %11.3f %6.1f %6.3f" % (dist1, kilo1, yard1, mile1)
kilo2=dist2*M2KILO
yard2=dist2*M2YARD
mile2=dist1*M2MILE
print "%6.0i %11.3f %6.1f %6.3f" % (dist2, kilo2, yard2, mile2)
kilo3=dist3*M2KILO
yard3=dist3*M2YARD
mile3=dist1*M2MILE
print "%6.0i %11.3f %6.1f %6.3f" % (dist3, kilo3, yard3, mile3)
kilo4=dist4*M2KILO
yard4=dist4*M2YARD
mile4=dist1*M2MILE
print "%6.0i %11.3f %6.1f %6.3f" % (dist4, kilo4, yard4, mile4)
lab2.4.withfunc.py 4/8
[top][prev][next]
# Displays equivalents for Olympic running distances in
# meters, kilometers, yards, miles
def metersToMiles(meters):
METERS_TO_MILES = .0006215
miles = meters * METERS_TO_MILES
return miles
# conversion constants
M2KILO=.001
M2YARD=1.094
# distances in meters
dist1=100
dist2=200
dist3=400
dist4=800
# print column headings
print "Meters Kilometers Yards Miles"
print "-"*32
# calculate conversions, print data for conversion table
kilo1=dist1*M2KILO
yard1=dist1*M2YARD
mile1=metersToMiles(dist1)
print "%6.0i %11.3f %6.1f %6.3f" % (dist1, kilo1, yard1, mile1)
kilo2=dist2*M2KILO
yard2=dist2*M2YARD
mile2=metersToMiles(dist2)
print "%6.0i %11.3f %6.1f %6.3f" % (dist2, kilo2, yard2, mile2)
kilo3=dist3*M2KILO
yard3=dist3*M2YARD
mile3=metersToMiles(dist3)
print "%6.0i %11.3f %6.1f %6.3f" % (dist3, kilo3, yard3, mile3)
kilo4=dist4*M2KILO
yard4=dist4*M2YARD
mile4=metersToMiles(dist4)
print "%6.0i %11.3f %6.1f %6.3f" % (dist4, kilo4, yard4, mile4)
menu.py 5/8
[top][prev][next]
STOP_OPTION = 'Q'
def printWelcomeScreen(name):
welcome = "Welcome to " + name + "!"
length = len(welcome)
print length*"-"
print welcome
print length*"-"
def printMenu():
print "You have some options for what to do: "
print "Enter an 'F' to find a song"
print "Enter an 'S' to sort by Song title"
print "Enter an 'A' to sort by Album"
print "Enter an 'R' to sort by aRtist name"
print "Enter an 'H' to list your options again"
print "Enter a 'Q' to quit"
menu_withfunctions.py 6/8
[top][prev][next]
# Using functions from menu module
# by Sara Sprenkle
from menu import *
printWelcomeScreen("MusicManager")
printMenu()
menuChoice = raw_input("Which option do you choose? ")
menuChoice = menuChoice.upper()
while menuChoice != STOP_OPTION :
printMenu()
menuChoice = raw_input("Which option do you choose? ")
menuChoice = menuChoice.upper()
menu_withoutfunc.py 7/8
[top][prev][next]
# What code would look like without functions
# by Sara Sprenkle
STOP_OPTION = 'Q'
name = "MusicManager"
welcome = "Welcome to " + name + "!"
length = len(welcome)
print length*"-"
print welcome
print length*"-"
print "You have some options for what to do: "
print "Enter an 'F' to find a song"
print "Enter an 'S' to sort by Song title"
print "Enter an 'A' to sort by Album"
print "Enter an 'R' to sort by aRtist name"
print "Enter an 'H' to list your options again"
print "Enter a 'Q' to quit"
menuChoice = raw_input("Which option do you choose? ")
menuChoice = menuChoice.upper()
while menuChoice != STOP_OPTION :
print "You have some options for what to do: "
print "Enter an 'F' to find a song"
print "Enter an 'S' to sort by Song title"
print "Enter an 'A' to sort by Album"
print "Enter an 'R' to sort by aRtist name"
print "Enter an 'H' to list your options again"
print "Enter a 'Q' to quit"
menuChoice = raw_input("Which option do you choose? ")
menuChoice = menuChoice.upper()
oldmac.py 8/8
[top][prev][next]
# Print out verses of the song Old MacDonald
# Sara Sprenkle
BEGIN_END = "Old McDonald had a farm"
EIEIO = ", E-I-E-I-O"
def main():
# call the verse function to print out a verse
printVerse("dog", "ruff")
printVerse("duck", "quack")
animal_type = "cow"
animal_sound = "moo"
printVerse(animal_type, animal_sound)
# QUESTION: What happens if main called function as
# printVerse("ruff", "dog")
# prints a verse of Old MacDonald, plugging in the animal and sound
# parameters (which are strings), as appropriate.
def printVerse(animal, sound):
print BEGIN_END + EIEIO
print "And on that farm he had a " + animal + EIEIO
print "With a " + sound + ", " + sound + " here"
print "And a " + sound + ", " + sound + " there"
print "Here a", sound
print "There a", sound
print "Everywhere a " + sound + ", " + sound
print BEGIN_END + EIEIO
print
main()
Generated by GNU enscript 1.6.4.