Contents
- birthyear.py
- string_compare.py
- string_iteration.py
- survey.py
birthyear.py 1/4
[top][prev][next]
# This program calculates your birthyear,
# given your age and the current year.
# Sara Sprenkle
import sys
print "This program determines your birth year"
print "given your age and current year."
print
age = input("Enter your age >> ")
if age > 120:
print "Don't be ridiculous. You can't be that old!"
sys.exit(1)
# input is reasonable ...
currentYear = input("Enter the current year >> ")
birthyear = currentYear - age
print
print "You were either born in", birthyear, "or",
print birthyear-1
string_compare.py 2/4
[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_iteration.py 3/4
[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])
survey.py 4/4
[top][prev][next]
# Demonstrate use of constants, string concatenation
# by CS111
import sys
SCALE_MIN=1
SCALE_MAX=10
DIVIDER_LENGTH=70
divider="-"*DIVIDER_LENGTH
print(divider)
# define prompt here
prompt = "On a scale of " + str(SCALE_MIN) + " to " + str(SCALE_MAX)
prompt += ", how much do you like Ryan Gosling? "
# ask once, with wise crack
rating = eval(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)
print(rating,"?!? That's more than I do.")
print(divider)
# ask again, with wise crack
rating = eval(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)
print("Nah,", rating, "is much too low.")
print(divider)
Generated by GNU enscript 1.6.4.