Wednesday, November 7, 2018

SQL in MySQL

Use Pythonclub3;
use Sakila;
Select * from Film;

/*this joins three tables*/
Select first_name, Last_name, title
From Actor
Join film_actor
On actor.actor_id=film_actor.actor_id
join Film
on film.film_id=film_actor.film_id
Where title='Academy Dinosaur';

Insert into Actor (first_name, Last_name)
values('HARRISON','FORD');

Select * from actor;

start transaction; 
Update Actor
Set First_name='ALEC'
Where actor_id=1;
commit;

Select * from Actor;

Start Transaction;
Update Actor
Set last_name='Smith';
rollback;

Tuesday, November 6, 2018

Indefinite Loops

First take

def main():
    #basic for loop (definite loops)
    for i in range(10):
       print("h1, vote")

    #indefinite loop
    total=0
    avg=0
    grade=0
    counter =0
    while grade >=0:
        grade=float(input("Enter grade -1 to exit: "))
        if grade ==-1:
            break
        total += grade
        #total = total + grade
        counter += 1
        print(counter)
    
    avg = total/counter
   
    print ("the average score is", avg)


main()

Menu loop

def menu():
    print ("Type the menu number for your option.")
    choice=0
    while choice !=5:
        print("1: birthday:")
        print("2: christmas: ")
        print("3: new years")
        print('4: thanksgiving')
        print('5: Exit')
        choice=int(input("Enter your choice"))

        if choice != 5:
            managechoices(choice)
        
def managechoices(choice):
    if choice==1:
        birthday()
    elif choice == 2:
        christmas()
    elif choice==3:
        newyears()
    else:
        thanksgiving()

def birthday():
    print("happy birthday")

def christmas():
    print ("merry christmas")

def newyears():
    print ("happy new years")

def thanksgiving():
    print( "I ate way too much")

def main():
    menu()

main()

      

Another example with grades and average using functions

def getGrade():
    grade=float(input("Enter a grade: "))
    return grade

def storeGrades():
    grades=[]
    grade=0
    print ("Enter grades. -1 to exit")
    #this is an example of a sentinal loop
    while grade >= 0:
        grades.append(grade)
        grade=getGrade()
    return grades

def getAverage():
    gradeList=storeGrades()
    avg = 0
    total=0
    for g in range(len(gradeList)):
        total += gradeList[g]
    avg=total / len(gradeList)
    return avg

def display():
    avg=getAverage()
    print ("the average grade is", avg)

def main():
    display()

main()

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()