Contents

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

assigningConstants.py 1/7

[
top][prev][next]
# In response to a question in class: 
# What happens if we try to assign a value to a math data value?

import math

print(math.e)

math.e = 1

print(math.e)

function_example.py 2/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 roundx
roundx = round(x)
print("x rounded to the nearest int:", roundx)

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

a = round(x, 2)
print("x rounded to the nearest hundredth:", a)

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

print(round(x, 4))

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

module_example.py 3/7

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

# Alternative: could 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("Your VA Pick4 number is", end=': ')

# generate four random numbers
for x in range(4):
    # generate a random number
    print( random.randint(0, 9), end=' ')
    
print()

# Why doesn't the following code work as an alternative correct solution?
#print(random.randint(0000,9999))


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 = 5
print("This program generates", NUM_RANDOM, "random numbers.")

# Demonstrates that it's a pseudo-random number generator
# If using the same seed, then 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

# Convention: constants are in all caps, at top of program
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("Input number: "))
    # add the user's input to my total
    total = num + total
    # total += num

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

# To address a question in class,
# you could print num and it would have the last value assigned to num: 
# print(num)

Generated by GNU Enscript 1.6.6.