'''
Author: Adam Cherochak for codeCampus (www.thecodecampus.com)
Purpose: Examples for reference. Made with Python version 2.7
___[ Additional Resources ]___
Online Python IDE: www.tutorialspoint.com/python
'''
# ___[ Example: Variables ]___
# Mad Libs
adjective1 = input("Enter an adjective: ")
noun1 = input("Enter a noun: ")
noun2 = input("Enter another noun: ")
adjective2 = input("Enter another adjective: ")
print "Roses are",adjective1
print noun1, "are blue"
print noun2, "is", adjective2
print "And so are you."
# ___[ Example: Conditionals ]___
# Secret Password
password = "baloney"
print("You have to say the secret password to enter the hideout!")
question = input("What is the secret password?")
if question == password:
print("Welcome to the hideout!")
else:
print("That's wrong! You can't enter.")
# ___[ Example: Conditionals ]___
# Colors and Rollercoaster
favorite_color = input("What's your favorite color? ")
if favorite_color == "red" or favorite_color == "blue":
print "That's one of my favorites too!"
else:
print "If you say so..."
# Rollercoaster
age = input("How old are you?")
height = input("How tall are you? (4.3 for four foot three inches, etc.)")
if age >= 9 and height >= 4.0:
print "you can get on the ride!"
else:
print "you can't get on the ride."
# ___[ Example: Loops ]___
# Dice Roll and Hangman
import random
rollAgain = "Y"
while rollAgain == "Y":
roll = random.randint(1,6)
print "You rolled a",roll,"!"
rollAgain = input("To roll again, enter 'Y'. To stop, enter 'N'")
if rollAgain == "N":
print "See ya later!"
# Hangman
name = raw_input("What is your name? ")
print "Hello,",name,", time to play hangman!"
print "\n"
print "Start guessing..."
word = "serendipity"
turns = len(word)
guesses = ''
playing = True
while playing:
failed = 0
for letter in word:
if letter in guesses:
print letter
else:
print "_"
failed += 1
if failed == 0:
print "\nYou won"
break
print
guess = raw_input("guess a character:")
guesses += guess
if guess not in word:
turns -= 1
print "Wrong\n"
print "You have", + turns, 'more guesses'
if turns == 0:
print "You Lose\n"
break
# ___[ Example: Loops ]___
# Dice Roll and Hangman Advanced
import random
rollAgain = "Y"
while rollAgain == "Y":
roll = random.randint(1,6)
print "You rolled a ",roll,"!"
rollAgain = raw_input("Type 'Y' to roll again, or 'N' to not.")
if rollAgain == "N":
print "See ya later!"
# Hangman Advanced
word = "banana"
turns = len(word)
guesses = ''
playing = True
while playing:
failed = 0
for letter in word:
if letter in guesses:
print letter
else:
print "_"
failed += 1
if failed == 0:
print "\nYou won"
break
print
guess = raw_input("guess a character:")
guesses = guesses + guess
if guess not in word:
turns -= 1
print "Wrong!"
print "You have", + turns, 'more guesses'
if turns == 0:
print "You Lose\n"
break
# ___[ Example: Play A Game ]___
# Attack
import random
myHealth = 100
monsterHealth = 100
print "A monster has appeared!"
while True:
print "Monster's health:",monsterHealth
print "My health:",myHealth
myAttack = random.randint(7,12)
myHeal = random.randint(5,15)
monsterAttack = random.randint(7,12)
monsterHeal = random.randint(5,15)
if myHealth <= 0 or monsterHealth <= 0:
print "GAME OVER"
break
else:
monsterMove = random.randint(1,2)
if monsterMove == 1:
print "MONSTER ATTACKS FOR",monsterAttack,"POINTS!"
myHealth = myHealth - monsterAttack
elif monsterMove == 2:
print "MONSTER HEALS FOR",monsterHeal,"POINTS!"
monsterHealth = monsterHealth + monsterHeal
if monsterHealth > 100:
monsterHealth = 100
print "Monster's health:",monsterHealth
print "My health:",myHealth
if myHealth <= 0 or monsterHealth <= 0:
print "GAME OVER"
break
else:
print "My moves:"
print "1: Attack. Attacks the monster for 7 to 12 damage"
print "2: Heal. Heals us for 5 to 15 points"
myMove = input("What would you like to do? Press 1 or 2.")
if myMove == 1:
print "YOU ATTACK FOR",myAttack,"POINTS!"
monsterHealth = monsterHealth - myAttack
elif myMove == 2:
print "YOU HEAL YOURSELF FOR",myHeal,"POINTS!"
myHealth = myHealth + myHeal
if myHealth > 100:
myHealth = 100
# ___[ Example: Play A Game ]___
# Battleship
from random import randint
#initializing board
board = []
for x in range(5):
board.append(["O"] * 5)
def print_board(board):
for row in board:
print row
print "Let's play Battleship!"
print_board(board)
#defining where the ship is
def random_row(board):
return randint(0, 5)
def random_col(board):
return randint(0, 5)
ship_row = random_row(board)
ship_col = random_col(board)
turns = 4
# Start main game loop
while True:
guess_row = input("Guess Row:")
guess_col = input("Guess Col:")
# if the user's right, the game ends
if guess_row == ship_row and guess_col == ship_col:
print "You sunk my battleship!"
break
else:
#warning if the guess is out of the board
if (guess_row < 0 or guess_row > 4) or (guess_col < 0 or guess_col > 4):
print "Out of bounds!"
#if the guess is wrong, mark the point with an X and start again
else:
print "You missed!"
board[guess_row][guess_col] = "X"
turns = turns - 1
# end the game if the user runs out of turns
if turns <= 0:
print "You lose!"
print "Battleship was at ",ship_row,",",ship_col
break
# otherwise, continue the game
else:
print "You have",turns,"turns left"
print_board(board)
# ___[ Example: Play A Game ]___
# Magic Bag
bag = []
adding = True
while adding:
ask = input("What do we need in our magic bag? Type 'Done' to exit. ")
if ask == 'Done':
print "Here's what's in your magic bag: ", bag
adding = False
else:
bag.append(ask)
# ___[ Example: Turtle Graphics ]___
# Turtle Graphicsimport turtle
# Draw a line
t = turtle.Turtle()
t.forward(50)
# Draw a square
"""
square = turtle.Turtle()
square.forward(50)
square.right(90) # Rotate clockwise by 90 degrees
square.forward(50)
square.right(90)
square.forward(50)
square.right(90)
square.forward(50)
square.right(90)
"""
# Draw a star using loops
"""
star = turtle.Turtle()
for i in range(50):
star.forward(50)
star.right(144)
"""
# Draw a spiral, add color
"""
painter = turtle.Turtle()
painter.pencolor("blue")
for i in range(50):
painter.forward(50)
painter.left(123) # Let's go counterclockwise this time
painter.pencolor("red")
for i in range(50):
painter.forward(100)
painter.left(123)
"""