Contents
- pick4num.py
- search.py
- string_iteration.py
pick4num.py 1/3
[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
GAME="VA Pick 4"
NUMFORMAT="####"
print("Let's play", GAME + "!")
# hardcode number first
pickedNum = "1234"
# pickedNum = input("What is your pick? (Format: " + NUMFORMAT + ") ")
###### TODO: handle bad input ######
print("The winning number is", end=" ")
lotteryNum = ""
# Generating the random number
for numPositions in range(NUM_PICKS):
nextNum = randint(MIN_VALUE, MAX_VALUE)
lotteryNum += str(nextNum)
#Alternatively: lotteryNum = lotteryNum + str(nextNum)
# Finish up:
# print the winning number
# determine if the user's number matches the winning number.
print()
search.py 2/3
[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 the above be an if-elif ?
# What is the impact of that decision?
string_iteration.py 3/3
[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])
Generated by GNU Enscript 1.6.6.