Alveyworld Inc.
  • Home
  • Apple Basics
  • Join Alveyworld

Start for the decode assignment

#second you need to write a decode function
def decode(line):
    #write the code to decode the message
    #remember to use ord() and chr()
    pass

#First thing is to connect to the file and read it.
file = open('message.txt', 'r')
for line in file:
   #Call your decode function here
   print line

Period 3 Pig Latin Translator

#Pig Latin translator
#igpay atinLay anslatortray
#(practice string slicing, and lists)

def pigify(word):
    vowel = 0
    for letter in word:
        if letter in 'aeiouyAEIOUY':
            start = word[:vowel]
            end = word[vowel:]
            return end+start+'ay'
        vowel+=1

words = ['Why', 'Panther', 'Turtle', 'Troy', 'Eraser', 'Wow', 'You', 'Music', 'Try', 'Dragon', 'Thirst', 'Thrust', 'Dynamic', 'Walrus', 'Symmetry', 'Dunces', 'Certification', 'Screen']

for word in words:
    print word, pigify(word)
  

Period 4 Pig Latin Translator

#pig latin translator
#igpay atinlay anslatortray

def pigify(word):
    for c in range(len(word)):
        if word[c] in 'aeiouyAEIOUY':
            start = word[:c]
            end = word[c:]
            return end+start+"ay"
           

words = ['racist','chair', 'potato', 'word', 'phat', 'strong', 'thrust', 'best', 'frog', 'aardvark', 'anteater', 'brown', 'white', 'giraffe', 'annoying']

for word in words:
    print pigify(word)
   
while True:
    word = raw_input("enter a word ")
    print pigify(word)

Prime number checking function

#Boolean Function to test Prime numbers
def is_prime(number):
    half = number/2
   
    if not number==2:
        if number%2==0:
            return False
   
    for odd in range(3,half,2):
        if number%odd==0:
            return False
   
    return True
   
print "2: ", is_prime(2)
print "3: ", is_prime(3)
print "5: ", is_prime(5)
print "61: ", is_prime(61)
print "291: ", is_prime(291)
print "321543: ", is_prime(321543)

Slope Intercept Drawing function

from turtle import *
import random

colormode(255)
width(4)
speed(0)

#funciton to draw a line in slope-intercept form
def graph_slope_intercept(m, b):
    up()
    for x in range(-50, 50):
        y = m*x + b
        goto(x,y)
        down()
       
#study changing slope   
for m in range(-200,200,20):
    red = random.randint(0,255)
    green = random.randint(0,255)
    blue = random.randint(0,255)
    pencolor(red,green,blue)
    graph_slope_intercept(m/100.0, 0)
   
#study changing intercept
for b in range(-200,200,20):
    red = random.randint(0,255)
    green = random.randint(0,255)
    blue = random.randint(0,255)
    pencolor(red,green,blue)
    graph_slope_intercept(1/2.0*-1, b)
    graph_slope_intercept(1/2.0, b)
input("stop it by hitting enter")

Random Triangles

from turtle import *
import random

p = Pen()
colormode(255)

def randcolor(pen):
    red = random.randint(0,255)
    green = random.randint(0,255)
    blue = random.randint(0,255)
    pen.pencolor(red, green, blue)

def triangle(pen, length, x, y):
    w = random.randint(3,10)
    randcolor(pen)
    pen.width(w)
    pen.up()
    pen.goto(x,y)
    pen.down()
    pen.fd(length)
    pen.right(120)
    pen.fd(length)
    pen.right(120)
    pen.fd(length)
   
    nextangle = random.randint(10,170)
    pen.left(nextangle)

#Review Loops
#iterate: repeat or loop
for counter in range(3,100):
    length = random.randint(100, 300)
    x = random.randint(-300,300)
    y = random.randint(-300,300)
    triangle(p, length, x, y)
   
input("U is better than Y")
   

The whole heart

#Happy Valentines Day.
from turtle import *
from math import *

fillcolor('red')
pencolor('black')
width(10)

#This code positions the starting point
up()
x = -pi/2
y = sin(x)
goto(y*50, x*50)
x += .01
down()

#this code draws the right side of the heart
begin_fill()
while x <= pi/2:
    y = sin(x)
    goto(y*50, x*50)
    x += .01
left(90)
circle(50, 180)

up()
x = -pi/2
y = sin(x)
goto(y*50, x*50)
x += .01
down()
left(90)

begin_fill()
while x <= pi/2:
    y = sin(x)
    goto(y*-50-100, x*50)
    x += .01
left(90)
circle(-50, 180)
end_fill()

input('Happy Valentines Day')

Mean, Median, Mode

ACT_scores = [345,345,3462,34,34,234,2,34,34]
#Mean-------------------------
total = sum(ACT_scores)
amount = len(ACT_scores)
mean = total/float(amount)
print "mean:", mean

#Median-----------------------
ACT_scores.sort()
middle = len(ACT_scores)/2
median = ACT_scores[middle]
print "median:", median

#Mode-------------------------number most
low = min(ACT_scores)
high = max(ACT_scores)

mode = 0
max_count = 0
for num in range(low,high):
    count = ACT_scores.count(num)
    if max_count < count:
        max_count = count
        mode = num

print "mode:",mode

Drawing Program

#Note: To run this program open the interpreter and type "from draw import *"
from turtle import *

ondrag(goto)

def jump(x, y):
    pu(); goto(x,y); pd()

onscreenclick(jump)
shape("circle")
pensize(5)
for n in "0123456789":
    onkey(lambda c=int(n): pensize(2*c+1), n)
listen()
speed(0)
pencolor("blue")

Random Pentagons

from turtle import * #direct import
import random  #indirect import
import math

p = Pen()
p.speed(10)

colormode(255)
p.width(5)

#Loops (iteration)
def pentagon(p, len, x, y):
    red = random.randint(0,255)
    green = random.randint(0,255)
    blue = random.randint(0,255)
    p.pencolor(red, green, blue)
    p.up()
    p.goto(x, y)
    p.down()
    for side in range(5):
        p.fd(len)
        p.right(72)
   
for dum in range(3,200):
    x = random.randint(-300, 300)
    y = random.randint(-300, 300)
    len = random.randint(5,20)
    pentagon(p, len*10, x, y)
   
input("I have a ferrari")
Powered by Create your own unique website with customizable templates.
  • Home
  • Apple Basics
  • Join Alveyworld