"""
CSCI 101 Iteration examples
"""
from random import *
def number_guessing_game():
"""
Implements a simple number-guessing game: picks a secret number, prompts
the users for guesses, each time telling them whether they've guessed too
high, too low, or correctly. Continues until a correct guess is given.
"""
# generate a random number
secret = randint(0, 100)
# initially, the guess is invalid
guess = -1
# loop until the user guesses the right number
while guess != secret:
guess = int(input('Guess a number: '))
if guess > secret:
print('too high')
elif guess < secret:
print('too low')
else:
print('correct!')
def number_guessing_game2():
"""
Implements a simple number-guessing game: picks a secret number, prompts
the users for guesses, each time telling them whether they've guessed too
high, too low, or correctly. Continues until a correct guess is given.
(Alternate version that uses a Boolean variable. Either version is fine.)
"""
# generate a random number
secret = randint(0, 100)
# initially, the guess is invalid
correct = False
while not correct: # same as "while correct == False"
# ask the user for a guess
guess = int(input("Guess a number: "))
# tell them where their guess lies relative to the answer
if guess > secret:
print("too high")
elif guess < secret:
print("too low")
else:
print("correct!")
correct = True
def investmentDoubles(rate):
"""
Return the number of years for an investment to double at given rate.
"""
initial = 100 # any initial value will work
current = initial
years = 0
while current < 2*initial:
current += rate * current # short for current = current + rate*current
years += 1 # short for years = years + 1
return years
def shift_letter(letter, num):
"""
Shifts a letter up num places in the alphabet with wraparound
"""
ALPHABET = "abcdefghijklmnopqrstuvwxyz "
# get the index of the current letter
index = ALPHABET.find(letter)
# we use the mod operator (%) for wraparound
return ALPHABET[(index + num) % len(ALPHABET)]
def fools_encrypt(msg):
"""
Duplicate each letter in msg 3 times
"""
secret = ''
for c in msg:
secret += c*3
return secret
def fools_decrypt(secret):
"""
Pull out every 3rd character from secret
"""
msg = secret[::3]
return msg
from turtle import *
import random
def draw_square(side_length):
forward(side_length)
right(90)
forward(side_length)
right(90)
forward(side_length)
right(90)
forward(side_length)
right(90)
def draw_square_with_loop(side_length):
for i in range(4): # do the following 2 indented lines 4 times
forward(side_length)
right(90)
def add_circles(number):
"""
Add number colored circles of radius 6 randomly through the screen
"""
x_range = int(window_width()/2)
y_range = int(window_height()/2)
for i in range(number):
x = random.randint(-x_range, x_range)
y = random.randint(-y_range, y_range)
# set the fill color of the circles (uncomment one of the following two):
setcolor_xy(x, y)
#setcolor_random()
pu()
goto(x,y)
pd()
begin_fill();
circle(6)
end_fill();
def setcolor_xy(x, y):
"""Set the fill color based on x, y coordinates"""
if x < 0 and y < 0:
fillcolor("blue")
elif x < 0 and y > 0:
fillcolor("purple")
elif x > 0 and y < 0:
fillcolor("red")
else:
fillcolor("yellow")
def setcolor_random():
"""Set the fill color randomly from: blue, purple, red and yellow"""
colornum = random.randint(1,4)
if colornum == 1:
fillcolor("blue")
elif colornum == 2:
fillcolor("purple")
elif colornum == 3:
fillcolor("red")
else: # colornum == 4
fillcolor("yellow")
# To try the code above, uncomment these lines
##tracer(False)
##add_circles(200)
##update()
##done()