Contents
- demo_str.py
- pick4winner_alt.py
- pick4winner_better_error_handling.py
- pick4winner.py
- pick4winner.py~
- search.py
- string_compare.py
- string_methods.py
- survey.py
demo_str.py 1/9
[top][prev][next]
# Demonstrate long strings and 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 \"\\\\\"")
pick4winner_alt.py 2/9
[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.
# Looks at each position of the winning number/user number.
# By CSCI111
from random import *
# 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 + ") ")
winningNum = ""
won = True
print("The winning Pick 4 lottery number is ", end='')
for i in range(NUM_PICKS):
randNum = randint(MIN_VALUE,MAX_VALUE)
print(randNum, end='')
# If they don't match, we know the user didn't win
if str(randNum) != pickedNum[i]:
won = False
print()
if won:
print("Congratulations! You are very lucky and rich!")
print("We should be friends!")
else:
print("Sorry, you lost.")
pick4winner_better_error_handling.py 3/9
[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 + ") ")
###### TODO: handle bad input ######
# Use of sys.exit(1) cleans up our program a bit.
# 1) Figure out if bad input, then exit the program
# 2) Continue with the code that works if we don't have errors.
if not pickedNum.isdigit(): # if pickedNum.isdigit() == False:
print("Your input contained non-digits")
sys.exit(1)
if len(pickedNum) != NUM_PICKS:
print("Check your input: should be", NUM_PICKS, "digits long")
sys.exit(1)
# accumulate the random number
# for a string, the equivalent of starting the accumulator at 0 is
# starting with an empty string:
winningNum = ""
for x in range(NUM_PICKS):
# create the random number and make it a string
randnum = str(randint(MIN_VALUE, MAX_VALUE))
# concatenate the newly generated number to the end of the
# winning number
winningNum = winningNum + randnum
print("The winning number is", winningNum)
if winningNum == pickedNum:
print("Congratulations! You won!")
else:
print("Sorry. Better luck next time!")
pick4winner.py 4/9
[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 *
# 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
pickedNum = input("What is your pick? (Format: " + NUMFORMAT + ") ")
###### TODO: handle bad input ######
if len(pickedNum) != NUM_PICKS:
print("Error! Incorrect length of number. Should be " + NUMFORMAT)
else:
winningNum = ""
for x in range(NUM_PICKS):
# create the random number
randNum = randint(MIN_VALUE, MAX_VALUE)
randNumAsStr = str(randNum)
winningNum = winningNum + randNumAsStr
# winningNum += randNumAsStr
#print(winningNum)
print()
print("The winning number is", winningNum)
if pickedNum == winningNum:
print("You won! :D Want to be friends?")
else:
print("You lost. :( Is this the best use of your $$?")
pick4winner.py~ 5/9
[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 *
# define constants that are easy to change so that our
# program is flexible
NUM_PICKS = 4
MIN_VALUE = 0
MAX_VALUE = 1
NUMFORMAT="####"
pickedNum = input("What is your pick? (Format: " + NUMFORMAT + ") ")
###### TODO: handle bad input ######
if len(pickedNum)!= 4:
print("Error!")
else:
winningNum = ""
for x in range(NUM_PICKS):
# create the random number
randNum = randint(MIN_VALUE, MAX_VALUE)
randNumAsStr = str(randNum)
winningNum = winningNum + randNumAsStr
# winningNum += randNumAsStr
#print(winningNum)
print()
print("The winning number is", winningNum)
if pickedNum == winningNum:
print("You won! :D Want to be friends?")
else:
print("You lost. :( Is this the best use of your $$?")
search.py 6/9
[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 = raw_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?
# What is the impact of that change?
string_compare.py 7/9
[top][prev][next]
# Program compares two strings
# by Sara Sprenkle
str1 = input("Enter a string to compare: ")
str2 = input("Compare '" + str1 + "' with what string? ")
print("-" * 40)
if str1 < str2 :
print("Alphabetically,", str1, "comes before", str2 + ".")
elif str1 > str2:
print("Alphabetically,", str2, "comes before", str1 + ".")
else:
print("You tried to trick me!", str1, "and", str2, "are the same word!")
string_methods.py 8/9
[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)
survey.py 9/9
[top][prev][next]
# Demonstrate use of constants, string concatenation
# by CS111
import sys
from random import *
SCALE_MIN=0
SCALE_MAX=100
DIVIDER_LENGTH=70
NUM_TIMES=3
divider="-"*DIVIDER_LENGTH
print(divider)
prompt = "On a scale of " + str(SCALE_MIN) + " to " + str(SCALE_MAX)
# broke up into 2 lines because ran out of room
prompt += ", what do you think of Ryan Gosling? "
for whichTime in range(NUM_TIMES):
# ask once, with wise crack
rating = float(input(prompt))
if rating < SCALE_MIN or rating > SCALE_MAX:
print("Your rating is not in the valid range", SCALE_MIN, "to", SCALE_MAX)
sys.exit(1)
responseType = randint(0,3)
if responseType <= 1:
print(rating,"?!? That's more than I do.")
elif responseType == 2:
print("yup, right on.")
else:
print("Nah,", rating, "is much too low.")
print(divider)
Generated by GNU Enscript 1.6.6.