Last active
August 29, 2015 14:10
-
-
Save wchan2/098830c1d8da7c9fc3b0 to your computer and use it in GitHub Desktop.
Introduction to Programming in Python Workshop
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def fizzbuzz(start, end): | |
i = start | |
fizzbuzz_dict = { | |
'five_three': [], | |
'five': [], | |
'three': [] | |
} | |
while i <= end: | |
if i % 5 == 0 and i % 3 == 0: | |
fizzbuzz_dict['five_three'].append(i) | |
elif i % 5 == 0: | |
fizzbuzz_dict['five'].append(i) | |
elif i % 3 == 0: | |
fizzbuzz_dict['three'].append(i) | |
i += 1 | |
return fizzbuzz_dict | |
fb_dict = fizzbuzz(10, 100) | |
print(fb_dict['five_three']) | |
print(fb_dict['five']) | |
print(fb_dict['three']) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def fizzbuzz(start, end): | |
i = start | |
fizzbuzz_list = [[], [], []] | |
while i <= end: | |
if i % 5 == 0 and i % 3 == 0: | |
fizzbuzz_list[0].append(i) | |
elif i % 5 == 0: | |
fizzbuzz_list[1].append(i) | |
elif i % 3 == 0: | |
fizzbuzz_list[2].append(i) | |
i += 1 | |
return fizzbuzz_list | |
fb_list = fizzbuzz(10, 100) | |
print(fb_list[0]) # values divisible by 5 and 3 | |
print(fb_list[1]) # values divisible by 5 | |
print(fb_list[2]) # values divisible by 3 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import random | |
class Game: | |
available_moves = ['rock', 'paper', 'scissors'] | |
winning_moves = { | |
'rock': 'paper', | |
'paper': 'scissors', | |
'scissors': 'rock' | |
} | |
def __init__(self, player_one, player_two): | |
self.player_one = player_one | |
self.player_two = player_two | |
def start(self): | |
print('\n\n====') | |
player_one_move = self.player_one.get_move() | |
player_two_move = self.player_two.get_move() | |
if player_one_move == player_two_move: | |
print('Result: Draw') | |
elif self.winning_moves[player_two_move] == player_one_move: | |
print('Result: ' + self.player_one.name + ' wins!') | |
else: | |
print('Result: ' + self.player_two.name + ' wins!') | |
print('====\n\n') | |
class HumanPlayer: | |
moves = ['rock', 'paper', 'scissors'] | |
def __init__(self, name): | |
self.name = name | |
def get_move(self): | |
move = raw_input('Please enter your move: ') | |
while move not in self.moves: | |
move = raw_input('You entered an invalid move. Please enter your move again: ') | |
print(self.name.capitalize() + ' played ' + move) | |
return move | |
class Computer: | |
moves = ['rock', 'paper', 'scissors'] | |
def __init__(self): | |
self.name = 'Computer' | |
def get_move(self): | |
move = self.moves[random.randint(0, 2)] | |
print(self.name + ' played ' + move) | |
return move | |
# calling code | |
game = Game(HumanPlayer('Will'), Computer()) | |
game.start() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment