Contents
- demo_str.py
- escape_sequence.py
- pick4winner_places.py
- pick4winner.py
- search.py
demo_str.py 1/5
[top][prev][next]
# Demonstrate long strings, escape sequences
# by Sara Sprenkle
string = """This is a long string.
Like, really long.
Sooooo loooooong"""
print(string)
print("To print a \\, you must use \"\\\\\"")
print("I could print more after this...", end="")
print("See?")
escape_sequence.py 2/5
[top][prev][next]
# Practice with escape sequences
# CS111
# Display To print a tab, you must use '\t'.
# If you use double quotes, you don't _need_ to escape the single quote
print("To print a tab, you must use a '\\t'.")
print('To print a tab, you must use a \'\\t\'.')
# Display I said, "How are you?"
print("I said, \"How are you?\"")
print('I said, "How are you?"')
pick4winner_places.py 3/5
[top][prev][next]
# Simulate Pick 4 lottery game - selecting ping pong balls at random
# Modified to count number of correctly chosen numbers
# 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="#" * NUM_PICKS
# get the user's input, as a string to maintain the four digits
userNumber = input("What is your pick (format: " + NUMFORMAT + ")? ")
# check if the user's number is valid
# Specifically, check if the user's number has the correct number of digits
if len(userNumber) != NUM_PICKS:
print("Error: incorrect number of digits")
print("You need", NUM_PICKS, "digits")
sys.exit()
# TODO: check if the user's number is only numbers
winningNum = ""
numMatches = 0
for whichPick in range(NUM_PICKS):
chosen = randint(MIN_VALUE, MAX_VALUE)
# concatenate the chosen number to the winning number
winningNum = winningNum + str(chosen)
if str(chosen) == userNumber[whichPick]:
# alternative, use winningNum[whichPick] instead of str(chosen)
numMatches = numMatches + 1
print("The winning Pick 4 number is", winningNum)
print()
# determine if the user won
if userNumber == winningNum:
print("Congratulations! You won!")
elif numMatches >= 1:
print("You matched", numMatches, "numbers. Collect your prize.")
else:
print("Sorry. You shouldn't be wasting your money anyway.")
pick4winner.py 4/5
[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="#" * NUM_PICKS
# get the user's input, as a string to maintain the four digits
userNumber = input("What is your pick (format: " + NUMFORMAT + ")? ")
# check if user number is valid
# Specifically, check if the user's number has four digits
if len(userNumber) != NUM_PICKS:
print("Error")
sys.exit()
# TODO: check if user number is all numbers
# accumulate the winning number as a string
winningNum = ""
for whichPick in range(NUM_PICKS):
chosen = randint(MIN_VALUE, MAX_VALUE)
# concatenate the chosen number to the winning number
winningNum = winningNum + str(chosen)
print("The winning Pick 4 number is", winningNum)
print()
# determine if the user won
if userNumber == winningNum:
print("Congratulations! You won!")
else:
print("Sorry. You shouldn't be wasting your money anyway.")
search.py 5/5
[top][prev][next]
# Demonstrate use of "in" operator for strings as well
# as an if test
# Sara Sprenkle
# QUESTION: Why is this a constant?
PYTHON_EXT = ".py"
filename = input("Enter a filename: ")
if filename[-(len(PYTHON_EXT)):] == PYTHON_EXT:
print "That's a name for Python script"
if PYTHON_EXT in filename:
print "That filename contains", PYTHON_EXT
# QUESTION: SHOULD THIS BE AN IF/ELIF ?
Generated by GNU enscript 1.6.4.