Contents
- binaryToDecimal.py
- non_function_vars.py
- oldmac.py
- pick4num_wfunctions.py
- practice1.py
- practice2.py
- scope.py
binaryToDecimal.py 1/7
[top][prev][next]
# Converts a binary number into a decimal
# By CSCI111, 02.13.2012
print("This program converts a binary number into a decimal number.")
# Read in the binary number as a string -- why?
binnum = input("Enter the binary #: ")
# accumulate the decimal value in this variable
decVal = 0
# go through the positions in the string
for pos in range(len(binnum)):
# num[pos] is a string; need to convert to an int
bit = int(binnum[pos])
# figure out which "place" the current bit is at
place = 2**(len(binnum)-pos-1)
# add to the decimal value
decVal += place * bit
print("The decimal value for", binnum, "is", decVal)
non_function_vars.py 2/7
[top][prev][next]
# Using variables that aren't part of any function
# by Sara Sprenkle
# create variables that aren't part of any function
non_func = 2
non_func_string = "aardvark"
def main():
func()
print(non_func)
print(non_func_string)
def func():
print("In func: nf =", non_func)
print("In func: nfs =", non_func_string)
# Question: what happens when we try to assign the variables that
# aren't part of a function a value?
non_func = 7
non_func_string = "zebra"
# Answer:
main()
non_func = 6
non_func_string = "dog"
print(non_func)
print(non_func_string)
main()
oldmac.py 3/7
[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")
def printVerse(animal, sound):
"""
prints a verse of Old MacDonald, plugging in the animal and sound
parameters (which are strings), as appropriate.
"""
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()
# Used to prevent automatically executing the main function when the
# program/module is imported.
if __name__ == '__main__':
main()
# main()
pick4num_wfunctions.py 4/7
[top][prev][next]
# Simulate Pick 4 lottery game - selecting ping pong balls at random
# Modified to figure out if the user entered the winning number
# By CSCI111
from random import *
import sys
# define constants that are easy to change so that our
# program is flexible
NUM_PICKS = 4
MIN_VALUE = 0
MAX_VALUE = 9
NUMFORMAT="#" * NUM_PICKS
def main():
# get the user's input, as a string to maintain the four digits
userNumber = input("What is your pick (format: " + NUMFORMAT + ")? ")
# check if user number is valid
# Specifically, check if the user's number has four digits
if len(userNumber) != NUM_PICKS:
print("Error")
sys.exit()
# TODO: check if user number is all numbers
winningNum = generateWinningNum()
print("The winning Pick 4 number is", winningNum)
print()
# determine if the user won
if userNumber == winningNum:
print("Congratulations! You won!")
else:
print("Sorry. You shouldn't be wasting your money anyway.")
def generateWinningNum():
"""
Generates and returns the winning number for the Pick4 game
"""
# accumulate the winning number as a string
winningNum = ""
for whichPick in range(NUM_PICKS):
chosen = randint(MIN_VALUE, MAX_VALUE)
# concatenate the chosen number to the winning number
winningNum = winningNum + str(chosen)
return winningNum
main()
practice1.py 5/7
[top][prev][next]
# Exercising your knowledge of variable scope.
#
def main():
num = eval(input("Enter a number to be squared: "))
squared = square(num)
print("The square is", squared)
def square(n):
return n * n
main()
practice2.py 6/7
[top][prev][next]
# Exercising your knowledge of variable scope.
def main():
num = eval(input("Enter a number to be squared: "))
squared = square(num)
print("The square is", squared)
print("The original num was", n)
def square(n):
return n * n
main()
scope.py 7/7
[top][prev][next]
# scope.py
# Program illustrating scope
# Note: NOT good coding style
# by Sara Sprenkle
def main():
n = 30
e = 0
f = 2
g = 3
h = 4
print("\nBefore the call to function1,")
print("n = ", n)
print("e = ", e)
# QUESTION: How to change function1's call to execute other branch?
i = function1(e, f, g, h)
print("\nAfter the call to function1,")
print("n = ", n)
print("e = ", e)
print("i = ", i)
def function1(a, b, c, d):
# QUESTION: What would happen if the following line was commented
# out?
n = 400
print("\nIn function1, ")
print("n = ", n)
print("a = ", a)
if a >= 1 :
a += b+n;
print("a = ", a, "after being modified")
return a
else :
c += d+n
return c
main()
Generated by GNU enscript 1.6.4.