Tuesday, October 30, 2018

Various IF statements

If stuff

def getNumbers():
    num1, num2=eval(input("Enter two numbers "))
    ans=divide(num1, num2)
    display(ans)

#def divide(number1, number2):
    #if number2 == 0:
       # print ("Can't divide by 0")
       # return
    #return number1/number2

def divide(number1, number2):
    if number2 != 0:
        return number1/number2
    else:
        print ("Can't divide by 0")
    return


def display(ans):
    print("the quotient is", ans)

def main():
    getNumbers()

main()
    

elif stuff

d#example of elif
from random import randrange

def getRandom():
    number=randrange(1,100)
    return number

def getGuess():
    guess=int(input ("Enter a number between 1, 100  "))
    return guess

def game():
    r = getRandom()
    for i in range(5):
        g=getGuess()
        result=evaluate(g, r)
        print(result)
        if result=="Congratulations": 
            print("the number was " + str(r))
            break



def evaluate(g, r):
    result=''
    if g > r:
        result='your guess is too high'
    elif g < r:
        result='Your guess is too low'
    else:
        result= 'Congratulations'
    return result

def main():
    game()

main()

#num=getRandom()
#print (num)

find max

myList=[3,5,24,31,6,7,53,9,14,8]

highest=myList[0]
for i in range(10):
    if myList[i] > highest:
        highest=myList[i]

print(highest)

Calculate pay

#overtime py
#calculates pay and overtime for a week

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

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

def calculatePay():
    rate=getWage()
    hrs=getHours()
    regpay=0
    pay=0
    OT=0
    if hrs > 40:
        #pay=rate * (40 + ((hrs-40)*1.5))
        regpay=rate * hrs
        OT=rate *(hrs-40)*1.5
        pay=regpay+OT
    else:
        regpay=rate * hrs
        pay=regpay
    display(regpay, OT, pay)

def display(reg, ot, pay):
    print("Your regular pay is", reg)
    print("Your overtime pay is",ot)
    print("Your total pay is", pay)

def main():
    calculatePay()

main()

Try example

num1=float(input("Enter a number "))
num2=float(input("Enter a number "))
try:
    ans=num1/num2
    print(ans)
except:
    print ("can't divide by 0")

Tuesday, October 23, 2018

functions

First Functions

#break into seperate
# each function should do one
 def Hello():
     print('Hello all you martians')

 def main():
     Hello()

 main()

Second Functions

def getName():
    name=input("enter your name")
    hello2(name)

def getAge():
    age=int(input("Enter your age: "))
    return age

def hello2(name):
    a=getAge()
    print ("Hello,",name,"you are",a,"years old")

def main():
    getName()

main()

Gas Mileage

#miles, gallons
#get inputs, calculate the mileage, output
def getMiles():
    miles=int(input("Enter the total miles: "))
    return miles

def getGallons():
    gallons=int(input("Enter the total gallons: "))
    return gallons

def calculateMileage():
    m=getMiles()
    g=getGallons()
    mpg=m/g
    output(m,g,mpg)

def output(miles, gallons, milesPerGallon):
    print ("You drove",miles,"miles.")
    print ("You put in",gallons,"gallons of gas")
    print("You had",milesPerGallon, "MPG.")

def main():
    calculateMileage()

main()

Meters to Feet

#get feet, calulate, output
def getFeet():
    feet=float(input("Enter the feet: "))
    return feet

def calcMeters():
    feet=getFeet()
    meters=feet / 3.2
    display(feet, meters)

def display(feet, meters):
    print(feet, "equals", meters,"meters")

def main():
    calcMeters()

main()

Monday, October 22, 2018

SQL Afternoon

use Community_Assist
Select * from person;
Select PersonLastname, Personfirstname,
  personemail from person
Select PersonLastname, Personfirstname,
  personemail from person
  order by personlastname

Select PersonLastname, Personfirstname,
  personemail from person
  order by personlastname desc

--Where clause, select among the rows
Select PersonLastname, Personfirstname,
  personemail from person
  Where personlastname='Smith'

Select PersonLastname, Personfirstname,
  personemail from person
  Where personlastname='Smith' 
  and Personfirstname ='Jerry'

Select * from Personaddress
Where not PersonAddressCity='Seattle'

Select * from personaddress
Where not PersonAddressApt is null

Select * from Donation 
where donationamount >= 1000

Select * From donation
Where donationdate='8/9/2015'

Select * From donation
Where donationdate 
between'8/9/2015' and '8/10/2015'
--join bringing tables together
Select * from GrantRequest

select grantrequestkey, grantrequestdate,
  Personlastname, personfirstname,
  granttypekey, grantrequestamount
  From Grantrequest
  inner join Person
  on person.personkey=grantrequest.personkey

  Select * 
  From Grantrequest
  inner join Person
  on person.personkey=grantrequest.personkey

  select grantrequestkey, grantrequestdate,
  Personlastname, personfirstname,
  granttypename, grantrequestamount
  From Grantrequest
  inner join Person
  on person.personkey=grantrequest.personkey
  inner join granttype
  On GrantType.GrantTypeKey=GrantRequest.GrantTypeKey

  --insert-- updates-- deletes
  --person, personaddress, donation
  Insert into person(PersonLastName, 
  PersonFirstName, 
  PersonEmail, 
  PersonEntryDate)
  Values('Smith','Fred','fsmith@fedex.com',
  GetDate())
  Select * from person

  Insert into PersonAddress
  (PersonAddressApt, 
  PersonAddressStreet, PersonAddressCity, 
  PersonAddressState, PersonAddressZip, 
  PersonKey)
  Values(null, '10001 Nowhere ave','Seattle',
  'WA', '9800', 130)

  Select * from Personaddress

  Insert into donation(Donationdate, 
  Donationamount, personkey)
  Values(getdate(), 1000.00, 130)

  Select * from donation

--update
Begin tran
Update person
Set personfirstname='Jason',
Personemail='Jason.Anderson@gmail.com'
Where personkey=1
Commit Tran

Select * from Person
Rollback tran

Delete from Person where personkey=130

Begin tran

Delete from donation

Select * from Donation

Rollback tran

SQL 1_morning

use Community_Assist;
--These are basic select statements
Select * from Person
Select personlastname, personfirstname,
   personemail from Person
Select personlastname, personfirstname,
   personemail from Person
   order by Personlastname
Select personlastname, personfirstname,
   personemail from Person
   order by Personlastname desc
--where clauses let you select rows
Select personLastName, personfirstname, personemail
From person
--character types are enclosed in single quotes
Where personlastname='Smith' and personfirstname='Jerry'
Select * from PersonAddress

Select * from PersonAddress 
where PersonAddressCity='Kent'

Select * from PersonAddress 
where Not PersonAddressCity='Seattle'

--returns nothing because time
--dates are single quoted
Select * from Donation 
Where DonationDate='8/9/2015'

Select * from Donation 
Where DonationDate between'8/9/2015' And '8/10/2015'

--numbers are not quoted
Select * from Donation 
Where donationamount > 1000

--null cannot use = > < 
Select * from personaddress
Where not PersonAddressApt is null

Select * from personaddress
Where  PersonAddressApt is null

--join tables: two tables
Select PersonlastName, personfirstname,
       Donationkey, Donationdate, Donationamount
From Person
Inner join Donation
ON person.personkey=Donation.personkey

Select * From Grantrequest

--three table join
Select GrantRequestDate, person.Personkey, PErsonlastname,
Personfirstname, Granttypename, grantrequestamount
From Grantrequest
inner join person 
On person.PersonKey=GrantRequest.personkey
inner join granttype
on granttype.GrantTypeKey=GrantRequest.GrantTypeKey

--insert update delete

--person, personaddress, donation
insert into person(personlastname, personfirstname,
PersonEmail,personentrydate)
Values('Tayor','Michael','mtaylor@gmail.com',
getdate())

Select * from PersonAddress

Insert into personaddress( 
PersonAddressApt, 
PersonAddressStreet, PersonAddressCity, 
PersonAddressState, PersonAddressZip, 
PersonKey)
Values(null,'1001 Somewhere else','Seattle',
'Wa','98001',130)

Insert into Donation(DonationDate, DonationAmount, personkey)
Values(getdate(), 250.00,130)

Select * from donation
--!!! update  updates are dangerous. If no where clause will change whole table!!!
begin tran--allows you to control the transactio
Update Person
Set personfirstname='Jason',
personemail='jason.anderson@gmail.com'
Where personkey=1
--you can only do one of the following
Rollback tran--the only way to undo
Commit tran--write the changes

--won't delete because person 130 has foreign keys in addres and donation
Delete from Person where personkey=130

Select * from person

begin tran
Delete from donation

--in a child table will delete everything
Select * from Donation

Rollback tran

Thursday, October 18, 2018

Peer Assignment 5, Assignment 3

#average word count

def main():
    sentence = input('Please enter a sentence: ')
    words=sentence.split()
    sum=0
    for word in words:
        sum += len(word)
    average = sum / len(words)
    average=round(average,0)
    print (' the sentence has', len(words), 'words')
    print ('the average word length is', average)

main()
#using ifs
def main():
    temp = float(input('Enter a temperature: '))
    #equality needs ==
    if temp==100:
        print('Jackpot')

    #actually from chapter 7
    if temp >= 90:
        print ("It is hot")
    elif temp >= 80:
        print ("it's warm")
    elif temp >= 70:
        print ("it's comfortable")
    else:
        print("it's cold")

main()

Tuesday, October 16, 2018

Strings, Lists, Files

String stuff

#strings, Lists, files
def main():
    mystring='This has already been a long morning.'
    print ('there are',len(mystring),'characters')
    print ('the 12th character is',mystring[12])
    for c in range(len(mystring)):
        print(mystring[c])
    midstring=mystring[9:17]
    print (midstring)
    wordlist=mystring.split()
    print(wordlist)
    print(wordlist[5])
    for w in range(len(wordlist)):
        print(wordlist[w])
    mystring=mystring.upper()
    print(mystring)
    mystring=mystring.lower()
    print(mystring)
    mystring=mystring.title()
    print(mystring)
    print (ord('a'))
    print (ord('A'))
    print(chr(97))
    print(chr(475))
    daylist='sun,mon,tue,wed,thurs,fri,sat'
    days=daylist.split(",")
    print(days)


main()

ListExamples

#Lists
def main():
    numbers=[1,37,4,4,2,1,9,10]
    sum=0
    for i in range(0,len(numbers)):
        sum += numbers[i]
    average=sum/len(numbers)
    print('the total is', sum, '\n' + 'the average is', average)

    mylist=[]
    for i in range(5):
       word=input("Enter a word: ")
        mylist.append(word)
    print(mylist)

    testgrade=[]
    num = int(input("How many grades: "))
    total=0
    
    for g in range(num):
        grade=float(input("Enter grade: "))
        testgrade.append(grade)
    
    print(testgrade)
    for g in range(0,len(testgrade)):
        total += testgrade[g]
    avg=total/len(testgrade)
    print("the average grade is", avg )

main()

FileExamples.py

#write and read a file
def main():
    outfile=open("C:\\temp\\outfile.txt", "w")
    print("this is my first sentence", file=outfile)
    print("this is my second sentence", file =outfile)
    outfile.close()

    infile=open('C:\\temp\\outfile.txt','r')
    for line in infile:
        print(line)
    infile.close()

main()

Monday, October 15, 2018

bit of SQL

Create Table Sale
(
  SaleID int identity(1,1) primary key,
  SaleDate DateTime default Current_timestamp,
  CustomerID int Foreign Key 
            references Customer(CustomerID)
);

Insert into Product(ProductName, ProductPrice)
Values('Plain donut',1.50),
('Cheese Danish', 3.25)

Select * from Product

Create Table SQL

Create Table Sale
(
   SaleID int identity(1,1) primary key,
   SaleDate Datetime default current_timestamp,
   CustomerID int Foreign key
       references Customer(CustomerID)
   --price decimal(6,2) not null
)

Thursday, October 11, 2018

Peer 4 exercise

#import graphics module
from graphics import *

#draw two circles in a ven diagram
def main():
    win=GraphWin("Ven",300,200)
    center=Point(100,100)
    radius=60
    circ=Circle(center, radius)
    circ.draw(win)
    center2=Point(200, 100)
    circ2=Circle(center2,radius)
    circ2.draw(win)
    win.getKey()

main()

Tuesday, October 9, 2018

graphics python

Vans.py

# determine the number of vans needed

def main():
    people=int(input("Enter the number of passengers: "))
    vanLoad=8
    numOfVans=people//vanLoad
    leftOver=people % vanLoad #modulus
    print("number of vans: ", numOfVans)
    print("Left over people", leftOver)
    #print ("If any people are left over order another van")
    if leftOver > 0:
        numOfVans = numOfVans + 1
    
    print ("you will need ", numOfVans, "vans")


main()

graphicsstuff.py

#import graphics
from graphics import *

def main():
    win =GraphWin("First graphics", 400,500)
    rec=Rectangle(Point(100,100), Point(300,300))
    rec.setFill('Blue')
    rec.draw(win)
    point=Point(200,200)
    circ=Circle(point,100)
    circ.setFill('Red')
    circ.draw(win)
    line = Line(Point(100,280), Point(280,280))
    line.setWidth(10)
    line.draw(win)
    win.getKey()

main()

ConvertForm.py

from graphics import *

def main():
    win=GraphWin("Celsius Converter", 400, 300)
    win.setCoords(0.0,0.0,3.0,4.0)
    Text(Point(1,3), "Celsius Temperature: ").draw(win)
    Text(Point(1,1), "Fahrenheit temperature: ").draw(win)
    inputText=Entry(Point(2.25,3),5)
    inputText.setText("0.0")
    inputText.draw(win)
    outputText=Text(Point(2.25,1), "")
    outputText.draw(win)
    button=Text(Point(1.5,2.0),"convert it")
    button.draw(win)
    Rectangle(Point(1,1.5), Point(2,2.5)).draw(win)

    win.getMouse()

    celsius = float(inputText.getText())
    fahrenheit=9.0/5.0 * celsius + 32

    outputText.setText(round(fahrenheit, 2))
    button.setText("Quit")

    
    win.getMouse()
    win.close()
main()

Thursday, October 4, 2018

Math python

CountChange.py

#program counts change
#int, float
import math

def main():
    print("Change Counter")
    print()
    print("please enter the count of coin type ")
    quarters = int(input ("Quarters: "))
    dimes=int(input("Dimes: ") )
    nickles=int(input("Nickles: "))
    pennies=int(input("Pennies: "))
    total = quarters * .25 + dimes * .10 + nickles * .05 + pennies * .01
    print()
    print("The total value is:  ", round(total,2))

main()

Here are are math excercises

import math

root = math.sqrt(144)
print(root)

#Integer Division
quotient = 12 // 7
remainder = 12 % 7
print("the quotient is ", quotient, "the remainder is ", remainder)
#float division
result = 12 / 7
print (result)
cube = 4 ** 3
print (cube)

Wednesday, October 3, 2018

Normalization. Afternoon

First Normal Form

all the data in an attribute must be of the same kind (logical, data type)

No lists or arrays

Track1, track2, track3. . .13

"track1,track2,track3..."

Second Normal Form

Assumes 1NF

Remove Structural dependencies, subsets, Groups of attributes that refer to each other but not to the topic (key) of the entity

Third Normal Form

Assumes 2NF

Remove transistive dependencies

One attribute that refers to another attribute

The Gist

Every Entity should be about one thing

Every attribute in the entity should describe that entity

Here is our final ERD

Normalization: Morning

Here are the rules for the first three normal forms

First Normal Form

Everything in a column must be of the same type (logically, datatype)

No Lists

No arrays

if lists or arrays break into a new entity

Second Normal Form

Get rid of subsets, groups of attributes that depend on each other rather than the topic (key) of the Entity (the group of advisor attributes that don't relate directly to student.)

Structural dependencies.

Third normal forms

- remove transitive dependency. One attribute that relates to something other than the topic of the entity.

The gist

Every column should have values of the same type

No arrays or lists (any plural values should be broken into their own entity)

An entity should only be about one thing

Anything that relates to something other than the topic of entity should be broken out into a new entity

Here is the student Entity as we started

Here is the diagram after normalizing

Tuesday, October 2, 2018

More python and loops:

Conversion.py

#conversion.py
# A program that converts celsius to fahrenheit
# by steve

#steps
#look up conversion formula
#get the celsius from input
#apply the equation (9/5)* c + 32)
# display the results

def main():
    print ("This program converts Celsius temperatures to Fahrenheit.")
    celsius=eval(input("Please enter a celsius temperature: "))
    fahrenheit = 9/5 * celsius + 32
    print("The fahrenheit temperature is", fahrenheit)

main()

Loops.py

# look at counted loops

def main():
    sum=0.0
    loops = int(input("How many numbers do you want to enter: "))
    for i in range(loops):
        number = int(input("Enter an integer"))
        sum =sum + number
    average=sum/loops
    print("the total is:",sum)
    print("the average is:",average)

main()

Monday, October 1, 2018

First Entity Relationships Afternoon

Here are the bakery entities and relationships

Here are the relationship diagrams

First ERDs mornings

Here is our little bakery ERD as far as we god

Here Are the diagrams for relationships