Contents

  1. pick4num.py
  2. sales_tax2.py
  3. sales_tax.py
  4. string_methods.py
  5. temp_table2.py
  6. temp_table.py
  7. widthvar.py

pick4num.py 1/7

[
top][prev][next]
# Simulate Pick 4 lottery game - selecting ping pong balls at random
# Modified to figure out if the user entered the winning number
# By CSCI111

from random import *
import sys

# define constants that are easy to change so that our
# program is flexible
NUM_PICKS = 4
MIN_VALUE = 0
MAX_VALUE = 9

NUMFORMAT="####"

pickedNum = input("What is your pick? (Format: " + NUMFORMAT + ") ")

######  handle bad input ######

# Check that user enters a string that contains only numbers
if pickedNum.isdigit() == False:  # alternatively: not pickedNum.isdigit()
    print("Error: Input must be numbers")
    sys.exit()

# If we get to here, we know the user's input is all digits

# User enters a number that is not four digits long
if len(pickedNum) != 4:
    print("Your number must contain four numbers")
    sys.exit()

# Generate the random number
winningNum = "" # start it as empty

for i in range(NUM_PICKS):
    # generate a random number
    # add the random number to the previous random number, as a str
    winningNum += str(randint(MIN_VALUE,MAX_VALUE))

print("The winning Pick 4 lottery number is ", winningNum)
print()

if winningNum == pickedNum:
    print("Congratulations!  You are very lucky and rich!")
    print("We should be friends!")
else:
    print("Sorry, you lost.")

sales_tax2.py 2/7

[
top][prev][next]
# Compute the cost of an item, plus sales tax
# Demonstrate need for/use of format specifiers
# by Sara Sprenkle

SALES_TAX=.05  # the sales tax in VA

value = eval(input("How much does your item cost? "))

with_tax = value * (1+SALES_TAX)

print("Your item that cost $%.2f" % value, end=' ')
print("costs $%.2f with tax" % with_tax)

sales_tax.py 3/7

[
top][prev][next]
# Compute the cost of an item, plus sales tax
# Demonstrate need for/use of format specifiers
# by Sara Sprenkle

SALES_TAX=.05  # the sales tax in VA

value = eval(input("How much does your item cost? "))

with_tax = value * (1+SALES_TAX)

print("Your item that cost $", value, end=' ')
print("costs $", with_tax, "with tax.")

string_methods.py 4/7

[
top][prev][next]
# Manipulate strings, using methods
# by Sara Sprenkle

sentence = input("Enter a sentence to mangle: ")

length = len(sentence)

# Question: What does the statement below do?
print("*", sentence.center(int(length*1.5)), "*")

print("Uppercase: ", sentence.upper())
print()
print("Lowercase: ", sentence.lower())
print()

# Answer before running...
print("Did sentence change?: ", sentence)

temp_table2.py 5/7

[
top][prev][next]
# Print out the table of temperatures
# By CS111

# print out the labels

underline = "-"*6

print("%6s %12s %12s" % ( "Temp F", "Temp C", "Temp K" ))
print("%6s %12s %12s" % ( underline, underline, underline))

# Better to calculate these conversions but that's not today's focus.
# The below code is also repetitive...  Why couldn't we simply use a loop?
# Good idea to use a constant for the template string, but I didn't
# want to hide the template strings.

ftemp = -459.67
ctemp = -273.15
ktemp=0

print("%6.1f %12.1f %12.1f" % ( ftemp, ctemp, ktemp))

ftemp = 0
ctemp = -17.77778
ktemp= 255.222

print("%6.1f %12.1f %12.1f" % ( ftemp, ctemp, ktemp))

ftemp = 32
ctemp = 0
ktemp= 273.15

print("%6.1f %12.1f %12.1f" % ( ftemp, ctemp, ktemp))


temp_table.py 6/7

[
top][prev][next]
# Print out the table of temperatures
# By CS111

# Better to calculate these conversions but that's not the focus today

# Some starter code; not filled in with printing the table.
# See temp_table2.py

ftemp = -459.67
ctemp = -273.15
ktemp=0

ftemp = 0
ctemp = -17.77778
ktemp= 255.222

ftemp = 32
ctemp = 0
ktemp= 273.15



widthvar.py 7/7

[
top][prev][next]
# String format whose width is a variable
# by Sara Sprenkle

word = input("Enter a word: ")

width = len(word)
# create the format specifier string with the length of the word
formatspec = "%" + str(width) + "i"

print()
print("Right justify based on length of the word")
print()

print(word)
print(width*"-")

for x in range(0, 101, 10):
    print( formatspec % x )


Generated by GNU enscript 1.6.4.