Monday, May 9, 2011

IT 104

Today there was a test for us. The purpose of the program we wrote was to keep track of 2 players playing golf and compare their scores after 9 holes. 20 minutes later here is the code I cranked out.

p1score = 0 #Tyler Cooper
p2score = 0 #5-09-11
hole = 1 #Holes start at hole 1, there is no hole 0
print "Enter Player 1 Name"
p1n = raw_input() #player 1 name
print "Enter Player 2 Name"
p2n = raw_input() #player 2 name
while (hole < 10):
     print "Enter number of strokes for", p1n, "on hole", hole
     p1score = p1score + input()
     print "Enter number of strokes for", p2n, "on hole", hole
     p2score = p2score + input() #Calculates new totals for scores
     hole = hole + 1 #Moves game to next hole
print p1n, "has", p1score, "strokes"
print p2n, "has", p2score, "strokes"
if (p1score < p2score):
     print p1n, "wins!"
if (p1score > p2score):
     print p2n, "wins!"
if (p1score == p2score):
     print "Draw game!"


Functions: Also called modules. Breaks programs into smaller peices
2 reasons to use:
  • manageability: its more efficient to manage a large program with functions

  • reusable: you can call it several times in the program

Use functions to do your math!

num1 = 0
num2 = 0
total = 0
print "Please enter the 1st number"
num1 = input()
print "Please enter the 2nd number"
num2 = input()
total = num1 + num2
print "Your total is", total

This is the main function. It's the one that runs first.

def addnum():
     num1 = 0
     num2 = 0
     total = 0
     print "Please enter the 1st number"
     num1 = input()
     print "Please enter the 2nd number"
     num2 = input()
     total = num1 + num2
     print "Your total is", total

codes for functions are also indented
addnum() is called a function call
anything outside of function is in the main! Main should always be below functions

def addnum(n1,n2):
     total = n1 + n2
     print "Your total is", total
num1 = 0
num2 = 0
total = 0
print "Please enter the 1st number"
;num1 = input()
print "Please enter the 2nd number"
num2 = input()

num1 and num2 are passed to n1 and n2 in the function
variables inside a function are considered localized. You can use the same variables in multiple functions. This will help keep code clean.


This is the code I wrote for the lab assignment
Purpose: create a program that will translate feet to inches

def feetToInch(feet):
     inches = feet * 12
     print "There are",inches,"inches in",feet,"feet"
foot = 0
print "How many feet are there?"

This was a terribly terribly easy code for me to write, but it is nice exercise to introduce me to functions.


Here's the code I wrote for the homework
Purpose: A program that takes 2 numbers and outputs the greater of the two

def greaterValue(num1,num2):
     if(num1 > num2):
          print num1, "is greater!"
     elif(num2 > num1):
          print num2, "is greater!"
          print "Both are great!"
first = 0
second = 0
print "Give me a number!"
first = input()
print "Give me another number!"
second = input()

Also terribly simple, and lots of fun with all the loops.

That's the end of my programming adventures for this week. Next week we will learn how to get values out of these functions and life will be great.


No comments: