Contents

  1. function_example.py
  2. lottery_winner.py
  3. module_example.py
  4. pick4.py
  5. print_examples.py
  6. random_test.py
  7. sumInputUsingConstants.py

function_example.py 1/7

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

x = 6.817454321
#x = 5.6512542

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

# Call the function round with input x
# Then, save 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 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))

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

lottery_winner.py 2/7

[
top][prev][next]
# Simulate a Lottery Winner Generator
# We generalized the program to make it easier to adapt for
# a variety of Lottery programs
# by CSCI111

import random

GAME_NAME="Megamillions"

NUM_PICKS=5
MIN_NUM_OPTION=0
MAX_NUM_OPTION=35

print("This program generates the", GAME_NAME, "lottery game winner.")

for x in range(NUM_PICKS): 
    print(random.randint(MIN_NUM_OPTION, MAX_NUM_OPTION), end=' ')
print()

module_example.py 3/7

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

# Alternative: could use import math
# Would then need to prepend all constants, functions with math.
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))

pick4.py 4/7

[
top][prev][next]
# Simulate the VA Pick4 Lottery game
# by CSCI111

import random

print("This program generates the VA Pick4 lottery game winner.")

print("The winning number is", end=' ')
for x in range(4): 
    print(random.randint(0, 9),end=' ')
    
# the following is needed to make sure that the next prompt follows on
# the next line.
print()

print_examples.py 5/7

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

print("Hi", "there", "class", sep='; ')
print("Put on same", end='')
print("line")

random_test.py 6/7

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

import random

NUM_RANDOM = 10
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(1, 10))


sumInputUsingConstants.py 7/7

[
top][prev][next]
# This program adds up numbers from the user.
# The number of numbers to sum up is provided by a constant variable 
# near the top of the program.  
# How does the constant make modifying the program easier?
# By CS111

NUM_INPUTS = 3

print("This program will add up", NUM_INPUTS, "numbers given by the user.")

total = 0

# repeat 5 times
for x in range(NUM_INPUTS):
    # get the input from the user
    num = float(input("Enter number: "))
    # add the user's input to my total
    total = num + total
    # total += num

print("The total of the inputted numbers is ", total)


Generated by GNU Enscript 1.6.6.