Contents

  1. animate.py
  2. function_example.py
  3. module_example_from_import.py
  4. module_example_import.py
  5. pick4.py
  6. print_examples.py
  7. random_test.py
  8. sum_nums.py

animate.py 1/8

[
top][prev][next]
# Simple demonstration of animation.
# by Sara Sprenkle

from graphics import *
from time import sleep

STEPS = 100

w = GraphWin("Simple Animation", 400, 400)
w.setBackground("orange")

current = Point(60,60)
circ = Circle(current, 50)
circ.setFill("blue")
circ.draw(w)

end = w.getWidth()
dx = (end - current.getX())/STEPS

for step in range(STEPS):
    circ.move(dx, 0)
    sleep(.1)

w.getMouse()
w.close()


function_example.py 2/8

[
top][prev][next]
# Examples using built-in functions
# Sara Sprenkle

#x = 6.817454321
x = 5.60123

print("We start with x having value", x)

# Call the function round with input x
# and save the output of function call in variable roundedXInt
roundedXInt = round(x)
print("x rounded to the nearest int:", roundedXInt)

roundedXTenth = round(x, 1)
print("x rounded to the nearest tenth:", roundedXTenth)

a = round(x, 2)
print("x rounded to the nearest hundredth:", a)
# demonstrating that the variable name doesn't matter,
# but good names make the code easier to understand

roundx = round(x, 3)
print("x rounded to the nearest thousandth:", roundx)

print(round(x, 4)) # not saving returned value in a variable

print("-"*40)
print("x is of", type(x))

module_example_from_import.py 3/8

[
top][prev][next]
# Example of importing a module
# by Sara Sprenkle

from math import *

i = 1j

# The equation e^(i pi) + 1 = 0

# with import math statement
# shouldbezero =  math.e ** (i * math.pi) + 1

shouldbezero =  e ** (i * pi) + 1

print("e^(i pi) + 1 equals", shouldbezero)

# practice using functions from modules
print("100^(1/2) =", sqrt(100))

module_example_import.py 4/8

[
top][prev][next]
# Example of importing a module
# by Sara Sprenkle

# Need to prepend all constants and functions with math.
import math

i = 1j

# The equation e^(i pi) + 1 = 0

# with import math statement
# shouldbezero =  math.e ** (i * math.pi) + 1

shouldbezero =  math.e ** (i * math.pi) + 1

print("e^(i pi) + 1 equals", shouldbezero)

# practice using functions from modules
print("100^(1/2) =", math.sqrt(100))

pick4.py 5/8

[
top][prev][next]
# Display the numbers that are selected by the magic 
# ping-pong ball machine for Pick4 VA Lottery, all on one line
# By CS111

import random

print("The winning Pick4 number is ", end="")

# generate the Pick4 number
for i in range(4):
	print(random.randint(0,9), end="")

print_examples.py 6/8

[
top][prev][next]
# Examples calling the print function
# Sara Sprenkle for CSCI111

print("Hi", "there", "class", sep='; ')
# By default end is "\n" --> called "the new line character"
# means, put the next displayed text on the next line.
print("Put on same", end='')
print("line")

# make end=" " (a space) instead:
print("Put on same", end=' ')
print("line")

something = 7

print("The result is ", something, ".", sep = "")

random_test.py 7/8

[
top][prev][next]
# Demonstrating random module
# by Sara Sprenkle

import random

NUM_RANDOM = 8
print("This program generates", NUM_RANDOM, "random numbers.")

# Demonstrates that it's a pseudo-random number generator
# If using the same seed, the program generates the same list of
# "random" numbers.

# The following function call sets the seed.
#random.seed(1)

for x in range(NUM_RANDOM):
    #print(random.random())
    print(random.randint(0, 10))


sum_nums.py 8/8

[
top][prev][next]
# Adding up numbers from the user
# By CSCI111

# initialize the accumulator variable
total = 0

# request that user says how many numbers they want to add up.
# (originally, we just had the user enter 5 numbers.)
numNums = int(input("How many numbers do you want to add up? "))


# loop until we're done
for i in range(numNums):
	number = int(input("What number do you want to add? "))
	# update the accumulator
	total = total + number
	# not needed but helpful in watching our program run
	print("Total so far is...",i,  total)
	
# display the results
print("Your total is", total)

Generated by GNU Enscript 1.6.6.