Contents

  1. demo_str.py
  2. pick4winner.py
  3. search.py
  4. string_methods.py

demo_str.py 1/4

[
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.py 2/4

[
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="####"

print("Let's Play the Pick4 Lottery!")

# start with a hardcoded number before getting user input
pickedNum = "1234"

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

######  TODO: handle bad input ######



print("The winning number is", end=" ")

for x in range(NUM_PICKS):
    randnum = randint(MIN_VALUE, MAX_VALUE)
    print(randnum, end="")

print()

search.py 3/4

[
top][prev][next]
# Demonstrate use of "in" operator for strings as well
# as an if test
# Sara Sprenkle

PYTHON_EXT = ".py"

filename = input("Enter a filename: ")

if PYTHON_EXT in filename:
    print "That filename contains", PYTHON_EXT


if filename[-(len(PYTHON_EXT)):] == PYTHON_EXT:
    print "That's a name for Python script"


# QUESTION: SHOULD THIS BE AN IF/ELIF?
# What is the impact of that change?




string_methods.py 4/4

[
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.