Contents
- pick4winner.py
- search.py
- string_compare.py
- string_iteration.py
- string_methods.py
pick4winner.py 1/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
# Why couldn’t we solve this before?
# What are valid choices for numbers?
# 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="####"
print("Let's Play the Pick4 Lottery!")
# start with a hardcoded number before getting user input
#pickedNum = "0000"
pickedNum = input("What is your pick? (Format: " + NUMFORMAT + ") ")
###### TODO: handle bad input ######
#
print("The winning number is", end=" ")
winNum = ""
for x in range(NUM_PICKS):
randnum = randint(MIN_VALUE, MAX_VALUE)
# concatenate the next random number to the winning number
winNum = winNum + str(randnum)
print( winNum)
# check if the winning number equals the picked number
if winNum == pickedNum:
print("You won!!!! Let's be friends!")
else:
print(":( wah wah")
search.py 2/5
[top][prev][next]
# Demonstrate use of "in" operator for strings as well
# as an if test
# Sara Sprenkle
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 3/5
[top][prev][next]
# Program compares two strings, updated to handle casing.
# by Sara Sprenkle
str1 = input("Enter a string to compare: ")
str2 = input("Compare '" + str1 + "' with what string? ")
print("-" * 40)
lowerStr1 = str1.lower()
lowerStr2 = str2.lower()
# use the lower-cased strings in the comparison but the *original* strings
# in the display
if lowerStr1 < lowerStr2 :
print("Alphabetically,", str1, "comes before", str2 + ".")
elif lowerStr1 > lowerStr2:
print("Alphabetically,", str2, "comes before", str1 + ".")
else:
print("You tried to trick me!", str1, "and", str2, "are the same word!")
string_iteration.py 4/5
[top][prev][next]
# Iterating through strings
# by Sara Sprenkle
phrase = input("Enter a phrase: ")
print("Iterate through phrase, using characters:")
for char in phrase:
print(char)
print()
print("Iterate through phrase, using positions of characters:")
for pos in range(len(phrase)):
print(pos, phrase[pos])
string_methods.py 5/5
[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)), "*")
upperSentence = sentence.upper()
print(upperSentence)
print(sentence)
print("Uppercase: ", sentence.upper())
print()
print("Lowercase: ", sentence.lower())
print()
# Answer before running...
print("Did sentence change?: ", sentence)
Generated by GNU Enscript 1.6.6.