Contents

  1. eval_cond.py
  2. string_compare.py
  3. survey.py

eval_cond.py 1/3

[
top][prev][next]
# Examples of advanced conditionals
# by Sara Sprenkle

x = 2
y = 3
z = 4

b = x==2
c = not b

# Note: because of precedence, we don't _need_ parentheses
# I added parentheses for readability/emphasis
d = (y<4) and (z<3)
print("d =", d)

d = (y<4) or (z<3)
print("d =", d)

d = not d
print(b, c, d)


string_compare.py 2/3

[
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!")

survey.py 3/3

[
top][prev][next]
# Demonstrate use of constants, string concatenation
# by CS111

import sys
from random import *

SCALE_MIN=0
SCALE_MAX=100
DIVIDER_LENGTH=70
NUM_TIMES=3

divider="-"*DIVIDER_LENGTH

print(divider)

prompt = "On a scale of " + str(SCALE_MIN) + " to " + str(SCALE_MAX)
# broke up into 2 lines because ran out of room
prompt += ", what do you think of Ryan Gosling? "

for whichTime in range(NUM_TIMES):
    # ask once, with wise crack
    rating = float(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)
    
    responseType = randint(0,3)
    if responseType <= 1:
        print(rating,"?!?  That's more than I do.")
    elif responseType == 2:
        print("yup, right on.")
    else:
        print("Nah,", rating, "is much too low.")
    
    print(divider)


Generated by GNU Enscript 1.6.6.