Contents
- pick4num.py
- string_methods.py
- temp_table2.py
- temp_table.py
pick4num.py 1/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 *
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 + ") ")
###### handle bad input ######
# Check that user enters a string that contains only numbers
if not pickedNum.isdigit():
print("Your number must contain only numbers")
sys.exit()
# If we get to here, we know the user's input is all digits
# User enters a number that is not four digits long
if len(pickedNum) != 4:
print("Your number must contain four numbers")
sys.exit()
# Generate the random number
winningNum = "" # start it as empty
for i in range(NUM_PICKS):
# generate a random number
# add the random number to the previous random number
winningNum += str(randint(MIN_VALUE,MAX_VALUE))
print("The winning Pick 4 lottery number is ", winningNum)
print()
if winningNum == pickedNum:
print("Congratulations! You are very lucky and rich!")
print("We should be friends!")
else:
print("Sorry, you lost.")
string_methods.py 2/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)), "*")
print("Uppercase: ", sentence.upper())
print()
print("Lowercase: ", sentence.lower())
print()
# Answer before running...
print("Did sentence change?: ", sentence)
temp_table2.py 3/4
[top][prev][next]
# Print out the table of temperatures
# By CS111
# print out the labels
underline = "-"*6
print("%10s %10s %10s" % ( "Temp F", "Temp C", "Temp K" ))
print("%10s %10s %10s" % ( underline, underline, underline))
# Better to calculate these conversions but that's not today's focus
ftemp = -459.67
ctemp = -273.15
ktemp=0
print("%10.1f %10.1f %10.1f" % ( ftemp, ctemp, ktemp))
ftemp = 0
ctemp = -17.77778
ktemp= 255.222
print("%10.1f %10.1f %10.1f" % ( ftemp, ctemp, ktemp))
ftemp = 32
ctemp = 0
ktemp= 273.15
print("%10.1f %10.1f %10.1f" % ( ftemp, ctemp, ktemp))
temp_table.py 4/4
[top][prev][next]
# Print out the table of temperatures
# By CS111
# Better to calculate these conversions but that's not the focus today
# Some starter code; not filled in with printing the table.
# See temp_table2.py
ftemp = -459.67
ctemp = -273.15
ktemp=0
ftemp = 0
ctemp = -17.77778
ktemp= 255.222
ftemp = 32
ctemp = 0
ktemp= 273.15
Generated by GNU enscript 1.6.4.