Thursday, October 26, 2017

Embedded if, first While loop

# if inside of ifs
# town that has a traffic fines
# $5 for each mile above the speed limit
# Maximum fine of $200

# input speed limit
# input the speed they were travelling

# test if speeding
# get the miles over speedlimit
# multiply each mile over by five
# if fine >200 then set to 200

def getSpeedLimit():
    limit = int(input("Enter the speed limit: "))
    return limit

def getSpeed():
    speed = int(input("Enter the speed traveled: "))
    return speed

def determineFine(limit, speed):
    penultyPerMPH=5
    fine =0
    if speed > limit:
        over = speed-limit
        fine = over * penultyPerMPH
        if fine > 200:
            fine=200
    return fine

def ticket(fine):
    if fine > 0:
        print("You owe", fine,"for speeding")
    else:
        print ("thank you for visiting")

        
def main():
    proceed="y"
    while proceed=="y":
        limit = getSpeedLimit()
        speed = getSpeed()
        fine = determineFine(limit, speed)
        ticket(fine)
        proceed=input("Continue y/n")
        proceed=proceed.lower()
        
    
main()




                  

Tuesday, October 24, 2017

Python If, slif and else

#Simple if

def main():
    number = int(input("Enter a number "))
    if number > 5:
        print ("the number is bigger than 5")
    elif number==5:
        print ("the number is 5")
    else:
        print("The number is less than 5")

main()

Here is the grading decision tree

#Grade scores between 100 and 0
#broken into 4 point system

def getScore():
    score=int(input("enter the test score "))
    return score

def getGrade(score):
    grade = 0.0
    if score >= 90:
        grade = 4.0
    elif score >= 85:
        grade = 3.5
    elif score >= 80:
        grade=3.0
    elif score >= 75:
        grade = 2.5
    elif score >= 70:
        grade = 2.0
    elif score >= 65:
        grade = 1.0
    else:
        grade= 0.0
    return grade

def printGrade(grade):
    print ("Your test grade is ", grade)

def main():
    testScore = getScore()
    #print(testScore)
    gradePoint = getGrade(testScore)
    printGrade(gradePoint)


main()

   
    

The prime number program

#Code will generate 41 prime number

def getNumber():
    number = int(input("Enter a number between 0 to 40: "))
    return number

def checkNumber(number):
    checkedNumber=0
    if number >=0 and number <=40:
        checkedNumber = number
    else:
        checkedNumber=-1
    return checkedNumber
    
        
def getPrime(number):
    prime = number * number - number +41
    return prime

def printPrime(prime):
    print ("The prime number is", prime)

def main():
    num = getNumber()
    checkNum = checkNumber(num)
    #print(checkNum)
    if checkNum == -1:
        print ("Numbers must be between 0 and 40")
        return
    prime = getPrime(num)
    printPrime(prime)

main()




        

Wages

#peer execise 6
#Wages with overtime

def getWage():
    rate = float(input("Enter your hourly rate"))
    return rate

def getHours():
    hours = float(input("Enter your weekly hours"))
    return hours

def calculateGross(rate, hours):
    gross=0
    overtimeRate=1.5
    regHours=40
    if hours > regHours:
        gross=rate * (regHours + ((hours-regHours)*overtimeRate))
    else:
        gross=rate * hours
    return gross

def printGross(gross):
    print("Your gross pay is",gross)

def main():
    rate = getWage()
    hours = getHours()
    gross=calculateGross(rate, hours)
    printGross(gross)


main()
    
    

Monday, October 23, 2017

SQL Code from Afternoon Class

Use Community_Assist;

Select PersonLastName, PersonFirstName, PersonEmail
From Person;

Select * from Person
Select PersonLastName, PersonFirstName, PersonEmail
From Person
order by PersonLastName 

Select PersonLastName, PersonFirstName, PersonEmail
From Person
order by PersonLastName desc

Select PersonLastName, PersonFirstName, PersonEmail
From Person
order by PersonLastName desc, PersonFirstName desc

Select PersonLastName, PersonFirstName, PersonEmail
From Person
Where PersonLastname='Tanner'
order by PersonLastName desc, PersonFirstName desc

Select * from Donation

Select DonationKey, donationDate, DonationAmount
From Donation
where donationAmount > 1000
--=, <, >, <=, >=, !=, <>, 

Select * from Donation where DonationDate='2015-8-11'

Select * from Donation where DonationDate 
between '2015-8-11' and '2015-8-12'

Select * from Donation where DonationDate not
between '2015-8-11' and '2015-8-12'

Select * from PersonAddress 
where Not PersonAddressCity = 'Seattle'
And PersonAddressApt is null

Select * from PersonAddress 
where Not PersonAddressCity = 'Seattle'
OR PersonAddressApt is null

Select * From Person where PersonLastName like 'T%r'

Select * From Person where PersonEmail like '%msn.com'

--Joins

Select * from Donation

Select PersonLastName, PersonEmail, DonationAmount
From Person
inner join Donation
on Person.PersonKey=Donation.PersonKey
Where PersonLastName='Mann'

Select PersonLastName, PersonEmail, sum(DonationAmount) as Total
From Person
inner join Donation
on Person.PersonKey=Donation.PersonKey
Where PersonLastName='Mann'
Group by PersonLastName, PersonEmail

Select * from GrantRequest
/* 
This is a multiline
comment
*/
Select PersonLastName, PersonEmail, GrantRequestDate,
GrantTypeName, GrantRequestAmount
From Person
inner join GrantRequest
on Person.PersonKey = GrantRequest.PersonKey
inner join GrantType
on GrantType.GrantTypeKey=GrantRequest.GrantTypeKey

Select DonationKey, DonationAmount, 
DonationAmount * .7 as ToCharity, 
DonationAmount * .3 as ToProgram
From Donation
--* mult, + add, - sub, / div, % modulus 

Insert into Person(PersonLastName, PersonFirstName, 
PersonEmail,  PersonEntryDate)
Values('Turner','Tina','tt@gmail.com',GetDate())

Insert into PersonAddress( 
PersonAddressStreet,PersonAddressZip, PersonKey)
Values('1000 Elsewhere blv', '98122', IDENT_CURRENT('Person'))

Insert into Donation(PersonKey, DonationDate, 
DonationAmount)
Values(IDENT_CURRENT('Person'),GetDate(),1000.00)

Select * from PersonAddress
Select * from Donation

SQL Code from Morning Class

Use Community_Assist;

/* first use of SQL
10/23/2017 */

Select PersonLastName, 
PersonFirstName, 
PersonEmail
From Person;

Select * from Person;

--sort
Select PersonLastName, PersonFirstName, PersonEmail
From Person
order by PersonLastName, PersonFirstName

Select PersonLastName, PersonFirstName, PersonEmail
From Person
order by PersonLastName desc,PersonFirstName 

Select PersonLastName, PersonFirstName, PersonEmail
From Person
where PersonLastName='Tanner'

--all last names with T
Select PersonLastName, PersonFirstName, PersonEmail
From Person
where PersonLastName like'T%'

Select * from Donation
Where not DonationAmount >= 1000
order by DonationAmount desc

Select * from Donation
where DonationDate = '2015-08-11 20:00:35:890'

Select * from Donation
where DonationDate between '2015-08-11' and '2015-08-12'

Select GetDate()

Select DonationAmount as Amount, DonationAmount * .65 as CharityPercent
From Donation
--*, +, -, /
--=, !=, <>, >, <, >=, <=

Select PersonLastname, PersonEmail, DonationAmount
From Person
inner Join Donation
on Person.PersonKey=Donation.Personkey
order by PersonlastName

Select PersonLastname, PersonEmail, Sum(DonationAmount)
From Person
inner Join Donation
on Person.PersonKey=Donation.Personkey
Group by PersonEmail, PersonLastName
order by PersonlastName

Select * from GrantRequest

Select PersonLastname, PersonFirstName, GrantRequestDate,
GrantTypeName, GrantRequestAmount
From Person
Inner Join GrantRequest
on Person.PersonKey=GrantRequest.PersonKey
Inner join GrantType
on GrantType.GrantTypeKey=GrantRequest.GrantTypeKey

Insert into Person(PersonLastName, PersonFirstName, 
PersonEmail, PersonEntryDate)
Values('Richards','Kieth','kr@gmail.com',GetDate())

Insert into PersonAddress(PersonAddressStreet, 
 PersonAddressZip, PersonKey)
Values('1000 Somewhere blvd', '98122',IDENT_CURRENT('Person'))

Insert into Donation(PersonKey, DonationDate, DonationAmount)
Values(IDENT_CURRENT('Person'),getDate(),5000.00)

Select * from Person
Select * from PersonAddress
Select * from Donation

Thursday, October 19, 2017

String and file stuff

#List a collection of values under one name

def main():
    dayNames=["Sun","Mon","Tue", "Wed", "Thu", "Fri", "Sat"]
    day = int(input(" Enter a number between 1 and 7 "))
    dayOfWeek = dayNames[day-1]
    print(dayOfWeek)

main()
    
    

Broken into functions

#Same program, but methods
#Define array
#get input
#Do the conversion number to word
#output

def setDays():
    dayNames=["Sun","Mon","Tue",
              "Wed", "Thu", "Fri", "Sat"]
    return dayNames

def getDay():
    day = int(input(" Enter a number between 1 and 7 "))
    return day

def getDayName(dayNumber):
    days=setDays()
    dayOfWeek = days[dayNumber-1]
    return dayOfWeek

def printDay(dayName):
    print (dayName)

def main():
    day = getDay()
    weekDay = getDayName(day)
    printDay(weekDay)

main()
    

File processing

#Files

def main():
    fileName=input("Enter File Name")
    inFile = open(fileName, "a")
    content = input("Write Something ")
    print(content, file=inFile)
    inFile.close()

    print("********************")
    inFile = open(fileName,"r")
    text=inFile.read()
    print (text)
    inFile.close()

main()

More Strings

#strings

def main():
    sentence=input("Enter a sentence  ")
    #print(sentence[0])
    #for i in range(0, len(sentence)):
       # print(ord(sentence[i]))
    #print(chr(962))
    #words=sentence.split(",")
    #for str in words:
        #print(str.upper())
    middle=sentence[5:10]
    print(middle)
    

main()

Tuesday, October 17, 2017

Functions from in class

#simple function

#creating a function
def cube():
    number = int(input("Enter a number "))
    myCube=number * number * number
    print ("the cube is", myCube)

def main():
    cube()#calling it in main

main()

Modified cube functions

#simple functions


def getInput():
    number = int(input("Enter a number "))
    return number

def cube(number):
    myCube = number * number * number
    return myCube


def printCube(myCube):
    print ("The cube is",myCube)

def main():
    number=getInput()
    myCube=cube(number)
    printCube(myCube)

main()
    

Kilometers conversion program

#conversion to kilometers
#get the input in miles
#calculation miles * 1.609
#print out results

def getMiles():
    miles=float(input("enter the miles "))
    return miles

def convert(m):
    kilometers=m * 1.609
    return kilometers

def printKilometers(k):
    print (k)

def main():
    mls = getMiles()
    ks = convert(mls)
    printKilometers(ks)

main()

Word length

main : getTotalWordLength()
TotalWordLength: getSentence()
   splitSentence()
                 calcAverage()
   printAverage()
#Figure out the average length of a word in a sentence
#Get the sentence
#split sentence into words
#loop through the words and get the lengths
#total the lengths
#divide the total length by the number of words
#print the average

def getSentence():
    sentence=input("Enter a sentence ")
    return sentence

def splitSentence():
    mySentence=getSentence()
    words=mySentence.split()
    return words

def getTotalWordLength():
    words=splitSentence()
    counter=0
    for i in range(0, len(words)):
        counter += len(words[i])
        #counter = counter + len(words[i])
    average=calcAverageWordLength(counter, len(words))
    return average

def calcAverageWordLength(total, sizeOfList):
    average = total/sizeOfList
    return average

def printAverage():
    print("The average word length in your sentence is",getTotalWordLength())

def test():
    #print(getSentence())
    #sentenceWords=splitSentence()
    #for i in range(0, len(sentenceWords)):
        #print(sentenceWords[i])
    #print(getTotalWordLength())
    print(getTotalWordLength())

#test()

def main():
    printAverage()


main()

Thursday, October 12, 2017

Functions

a simple function

#Simple function

#define function
def cube():
    number=int(input("Enter a number"))
    myCube=number * number * number
    print (myCube)

def main():
    cube()#call function
main()

A revision of the cube function that takes a parameter

#Simple function

#define function
def getNumber():
    number=int(input("Enter a number "))
    cube(number)#call number and pass a value
    
def cube(num):
    myCube=num * num * num
    print (myCube)

def main():
    getNumber()#call function

main()
            

another version with the cube function returning a value

#functions with passing a value
#and returning values

#define function
def getNumber():
    number=int(input("Enter a number "))
    return number
    
def cube(num):
    myCube=num * num * num
    return myCube

def printResults():
    number=getNumber()
    print("the cube is",cube(number))#calls cube
    

def main():
    printResults()

main()
            

A series of functions with one returning a value.

#using functions
# this program uses a function
#to gather some text
#another to write it to a file
#another to read the file
#and a main function to call
#the functions
#steve conger 10/12/2017

# function to get text
def getText():
    myText=input("enter your text ")
    return myText #returns the text
                  #to the callling function

def writeFile():
    text=getText() #calls getText() assigns
                   #returned value
    #get filename
    fname=input("enter file name")
    #open a file for writing
    outfile=open(fname,"w")
    #print the text to the file
    print(text, file=outfile)

def readFile():
    #gets file name
    fname=input("enter file name")
    #opens the file for reading
    infile=open(fname,"r")
    #reads what's in the file
    data = infile.read()
    #prints output
    print(data)

def main():
    writeFile() #calls writeFile()
    #no need to call getText() because
    #it is called in writeFile()
    #this is just for seperation
    print("**********************")
    readFile() #call readFile()

main()#call main

Tuesday, October 10, 2017

More strings and peer exercise 4

Here are the string practices complete with errors

Python 3.6.1 (v3.6.1:69c0db5, Mar 21 2017, 17:54:52) [MSC v.1900 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> 
======== RESTART: C:/Users/sconger/Documents/Python Programs/vans.py ========
You will need 4 Vans
>>> 
======== RESTART: C:/Users/sconger/Documents/Python Programs/vans.py ========
You will have 3 full vans
You will have one van with 5 Passengers
You will need 4 Vans
>>> #string is list of characters
>>> myString="the quick fox jumped over the lazy brown dog"
>>> print(myString[10])
f
>>> print(len(myString))
44
>>> for i range(0,len(myString)-1)
SyntaxError: invalid syntax
>>> for i range (0, len(myString)-1):
 
SyntaxError: invalid syntax
>>> for i in range (0, len(myString)-1):
 print(myString[i])

t
h
e
 
q
u
i
c
k
 
f
o
x
 
j
u
m
p
e
d
 
o
v
e
r
 
t
h
e
 
l
a
z
y
 
b
r
o
w
n
 
d
o
>>> for i in range (0, len(myString)):
 print(myString[i])

t
h
e
 
q
u
i
c
k
 
f
o
x
 
j
u
m
p
e
d
 
o
v
e
r
 
t
h
e
 
l
a
z
y
 
b
r
o
w
n
 
d
o
g
>>> print (myString[10,13])
Traceback (most recent call last):
  File "<pyshell#12>", line 1, in <module>
    print (myString[10,13])
TypeError: string indices must be integers
>>> print (myString[10:13])
fox
>>> wordList = myString.split()
>>> for str in wordList:
 print(str)

the
quick
fox
jumped
over
the
lazy
brown
dog
>>> print (len(wordList))
9
>>> print(wordList[3])
jumped
>>> 
======== RESTART: C:/Users/sconger/Documents/Python Programs/peer5.py ========
enter a sentenceIt was an existential crises for many
the average word is  4.428571428571429 characters long
>>> 
======== RESTART: C:/Users/sconger/Documents/Python Programs/peer5.py ========
enter a sentence a to the four fives
the average word is  3.0 characters long
>>> 

Here is the peer exercise 5

#peer exercise 5

def main():
    mySentence = input("enter a sentence")
    wordList = mySentence.split()
    counter=0
    for i in range(0,len(wordList)):
        wordLength=len(wordList[i])
        #counter=counter + wordLength
        counter += wordLength
    average = counter/len(wordList)
    print ("the average word is ",average,"characters long")

main()

Sunday, October 8, 2017

basic string functions

Python 3.6.0 (v3.6.0:41df79263a11, Dec 23 2016, 08:06:12) [MSC v.1900 64 bit (AMD64)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> sentence = "This is a string."
>>> letter =sentence[3]
>>> print (letter)
s
>>> print(len(sentence))
17
>>> words=sentence[5:10]
>>> print (words)
is a 
>>> wordList=sentence.split()
>>> print (wordList[3])
string.
>>> print (chr(960))
Ï€
>>> print (ord("m"))
109

>>> sentence2 ="This is another sentence"
>>> for c in range(0,len(sentence2)):
 print(sentence2[c])

T
h
i
s
 
i
s
 
a
n
o
t
h
e
r
 
s
e
n
t
e
n
c
e
>>> mylist=["one","Two", "Three", "Four", "Five"]
>>> for str in mylist:
 print(str)

one
Two
Three
Four
Five

Peer exercise 3

Peer exercise three involved something we haven't covered yet: an if statement. That was my mistake, but I wanted to show you what it would look like."

Here is one way to code that exercise.

# this program uses integer division
#to determine how many vans
#it will take to transport several people
#steven conger 10/8/2017

def main():
    passengers=29
    vanCapacity=8
    print ("For our trip we will have ", passengers, "passengers.")
    print ("Each van can take", vanCapacity, "of them")
    vans = passengers // vanCapacity #integer division
    print (vans, " Will be full.")
    
    extra = passengers % vanCapacity #modulus
      

    #This part requires and if something we really
    #haven't covered yet
    if extra != 0:
        vans=vans + 1
        print ("One van will have ", extra, "passengers")

    print("You will need ", vans, " vans.")
    

main()
    

Here is the result screen

>>> 
== RESTART: C:/Users/steve_conger/OneDrive/Documents/PythonPrograms/vans.py ==
For our trip we will have  29 passengers.
Each van can take 8 of them
3  Will be full.
One van will have  5 passengers
You will need  4  vans.
>>> 

Thursday, October 5, 2017

In class code Numbers and graphics

Python 3.6.1 (v3.6.1:69c0db5, Mar 21 2017, 17:54:52) [MSC v.1900 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> num1=5
>>> num2=7
>>> product = num1 * num2
>>> print (product)
35
>>> #+, -, *, /
>>> quotient = 23 / 3
>>> print (quotient)
7.666666666666667
>>> quotient = 23 // 3
>>> print (quotient)
7
>>> remainder=23 % 3
>>> print (remainder)
2
>>> number = eval(input("Enter a number "))
Enter a number 5
>>> square = number * number
>>> print (square)
25
>>> number = float(input("Enter a number "))
Enter a number 5
>>> print number
SyntaxError: Missing parentheses in call to 'print'
>>> print (number)
5.0
>>> number = int(input("Enter a number " ))
Enter a number 5
>>> print(number)
5
>>> 
#This program will show
#Some of the graphics features
#of the graphics libray
#Steve 10/5/2017

#import graphics
from graphics import *

def main():
    win=GraphWin ("Drawings",width=500, height=500)
    p=Point(50,60)
    p.draw(win)
    p3=Point(160,90)
    p3.draw(win)
    line=Line(p,p3)
    line.draw(win)
    center=Point(100,100)
    circ = Circle(center, 30)
    circ.setFill("blue")
    circ.draw(win)
    p4=Point(20,50)
    p5=Point(80,150)
    oval=Oval(p4,p5)
    oval.draw(win)



main()

Wednesday, October 4, 2017

Design and Normalization

I have combined the materials for both the morning and afternoon classes

The Access example

The Author Table

The book table

The AuthorBookTable

Query Results