Code Monkey home page Code Monkey logo

teals's Introduction

Intro to Computer Science (Python)

Dan Grecoe - A Microsoft Employee

This repo was put together for my first teaching experience with Teals with Mr. Norton from Essex North Shore.

This repository is meant to help beginner Computer Science students with Python.

Contents

Name Type Content
0_python Directory Introduction to basic Python programming structures and general programming.
1_practice Directory Some practice scripts for topics covered in 0_python.
2_student_programs Directory Programs to help the student hone their knowledge of concepts learned in the classroom.
3_example_programs Directory Contains example programs using concepts covered in class. Other programs are added for the student to learn if they choose to.
4_advanced Directory Advanced Python toopics that are not covered in the intro class, but can be useful for more advanced students (or for when I forget how to do something!).
5_presentations Directory Presentations prepared for classes.
6_deep_projects Directory Advanced command line program applications and the supporting files to implement them.
7_testing Directory Automated testing application for Python files as a teaching aide for automated testing.
README.md file This file you are reading right now.

Getting code locally

  1. Create a directory on your computer. Name it anything you want, but make sure you remember it.
  2. Open a command prompt, bash, or Powershell.
  3. Use the change directory (cd) command to point to the directory you created.
    • Example: cd c:\teals
  4. Issue the following command in whatever prompt you used to create the directory.
    • git clone https://github.com/grecoe/teals.git

Updating code locally

  1. Go to the directory you cloned the repo on the command line.
  2. Type the command git pull

Using this repository

This repository attempts to organize the learning of the Python programming language.

Starting in 0_python, students can learn the basic building blocks of the language.

Prerequisites

To make use of this repository it is recommended that the students have the ability to run the Python code on their own machines. It is possible, of course, to use online Python tools as well, but having the code locally means you can work on your Python skills without having to have any external connection.

  1. Install Python 3.5 or higher
  2. Install Visual Studio Code (NOTE: This will run on Windows, Mac or Linux)
  3. Clone this repository to your local machine. Note the directory in which the code is downloaded to.
  4. Open Visual Studio Code and from the file menu, select Open Folder choosing the folder in which you downloaded this repository to.

Running the code

In Visual Studio Code you will have a list of files to the left and two panes on the right. The top pane being the code (or whatever) file you are looking at, the bottom having a multi tabbed view with one of them being the terminal.

Change a directory and run a file by typing in the terminal window

cd 0_python
python 00_parameters.py

To vew the conents of the file, navigate to teh file in the left pane.

teals's People

Contributors

grecoe avatar

Watchers

 avatar  avatar

teals's Issues

Code not running properly.

hour = input("What hour of the day is it? ex:10am or 6pm: ")

if hour == "7am" :
print("You should be getting ready for school.")

elif hour == "8am" or "9am" or "10am" or "11am" or "1pm" or "2pm" :
print("You should be at school.")

elif hour == "12pm" :
print("You should be at lunch.")

elif hour == "3pm" or "4pm" or "5pm" or "6pm" or "7pm" or "8pm" or "9pm" :
print("You should be home, out of school")

else:
print("You should be asleep.")

No matter what time I put in, except 7am, the code will always print "You should be at school" and I can't figure out why. If you are able to figure it out can you please email me at [email protected].
Thank you,
Joey Bono

(WAR Card Game) Game Outline

With the other Code Samples added to your game.py file, here is the outline of the game play itself:

'''
    Game play.

    Add in a max turns because sometimes it just doesn't get there quickly. 
'''
# Create the player decks
players = createDecks(7)
# Track number of turns
turns = 0
# Artificial limit in case there are no winners by this number
max_turns = 400

'''
    Game play here: Game ends when EITHER of the player lists is empty, so 
    we create a while loop that simply checks that both player lists are 
    not empty. 
'''
while len(players[0]) and len(players[1]):
    '''
        Track the number of turns, if we hit the maximum, just quit. Occasionally, 
        this case will be hit as the randomness of this game can produce really long
        games. 
    '''
    turns += 1
    if turns >= max_turns:
        break

    '''
        Take a card from the top of each players deck (list), then 
        add them to the current_cards list. These are cards that will
        eventually go to the winner of the match. 
    '''
    p1Card = players[0].pop()
    p2Card = players[1].pop()
    current_cards = [p1Card, p2Card]
    print("P1 = {}, P2 = {}".format(p1Card,p2Card))

   # ADD GAME CODE HERE
}

(WAR Card Game) Create the player decks

Function to create the player decks....using random.shuffle() to shuffle them.

import random

def createDecks(shuffle_count = 7):
    '''
        Create a 52 card deck, shuffle it, and return two even decks.

        ARGUMENT:
            shuffle_count : Number of times to shuffle the deck, default value
                            is 7, so if no value is provided when calling the function
                            the deck will be shuffled 7 times. Otherwise, it is shuffled
                            the number of times the caller indicates. 

        RETURNS:
            A list of lists : [[0][1]].

            List[0] = Player 1
            List[1] = Player 2
    '''

    '''
        Deck of cards is made of 4 suits: Diamonds, Hearts, Clubs Spades

        Card values are 2-14
            2-10 for 2-10
            11-14 for J, Q, K, A

        Build a deck with 4 sets (one for each suit) of 2-14
    '''
    card_face = [2,3,4,5,6,7,8,9,10,11,12,13,14]
    deck = []
    for i in range(4):
        for card in card_face:
            deck.append(card)

    '''
        Now we need to shuffle the deck we have. Shuffle as many
        times as shuffle_count indicates (whose default is 7) 
    '''
    for i in range(shuffle_count):
        random.shuffle(deck)

    '''
        Create the player decks, one for each of the two players. These
        are created as lists within another list. 

        The loop goes over the entire deck that we created using the modulo (%) 
        operator. Using N % 2 will return either a 0 or 1 allowing us to evenly
        distribute the cards to each player list (deck).
    '''
    players = [[],[]]
    for i in range(len(deck)):
        players[i % 2].append(deck[i]) 

    '''
        Finally, return the two player decks (lists) contained in one outer list.
    '''
    return players   

TEALS Suggestion - Loops/Dictionaries

Move nested for loops earlier in the syllabus. Multiple loops are useful for the TicTacToe to finding the winner.

Dictionaries are a pretty important part of Python programming and having a section on this would be really helpful to help students better organize settings and the like.

(WAR Card Game) Pre-pending cards

When we have to add cards back to the winners deck, we don't want to add them to the top of the deck, but the bottom, this function will help with that.

def prependCards(deck, cards):
    '''
        We don't want to just add the win cards to the top of the deck, they should be added to 
        the bottom of the deck. 
        
        To do so we simply add each of the cards to the start of the deck.

        ARGUMENTS:
            deck    : Player deck(list) we want to add cards to
            cards   : List of cards to add to deck.

        RETURN:
            Nothing
    '''
    while len(cards):
        deck.insert(0,cards.pop())

Resulting War Cards Not Added to Deck (players[0])

My problem is that during doWar when either player 1 or 2 wins the cards, they are not added to the decks.

Issue:(Terminal)
[4, 5, 14, 13, 12, 2, 12, 4, 5, 8, 2, 6, 8, 2, 14, 12, 11, 11, 11, 7, 4, 3, 10, 7, 5, 9]
[10, 2, 7, 6, 13, 13, 3, 13, 4, 5, 11, 6, 10, 14, 9, 14, 9, 6, 3, 12, 8, 8, 9, 10, 3, 7]
Turn 1
P1 = 2, P2 = 4
Player 1 Cards Total 25
Player 2 Cards Total 25
Turn 2
P1 = 5, P2 = 11
Player 1 Cards Total 24
Player 2 Cards Total 26
Turn 3
P1 = 13, P2 = 6
Player 1 Cards Total 23
Player 2 Cards Total 27
Turn 4
P1 = 5, P2 = 9
Player 1 Cards Total 24
Player 2 Cards Total 26
Turn 5
P1 = 14, P2 = 3
Player 1 Cards Total 23
Player 2 Cards Total 27
Turn 6
P1 = 5, P2 = 3
Player 1 Cards Total 24
Player 2 Cards Total 26
Turn 7
P1 = 9, P2 = 10
Player 1 Cards Total 25
Player 2 Cards Total 25
Turn 8
P1 = 12, P2 = 9
Player 1 Cards Total 24
Player 2 Cards Total 26
Turn 9
P1 = 8, P2 = 14
Player 1 Cards Total 25
Player 2 Cards Total 25
Turn 10
P1 = 11, P2 = 13
Player 1 Cards Total 24
Player 2 Cards Total 26
Turn 11
P1 = 6, P2 = 4
Player 1 Cards Total 23
Player 2 Cards Total 27
Turn 12
P1 = 10, P2 = 10
Player 1 Cards Total 24
Player 2 Cards Total 26
war
#each time there is a war the total cards of players 1 and 2 go down by 10 (50 to 40 to 30 and so on, 2 cards are face up)
Turn 13
P1 = 14, P2 = 2
Player 1 Cards Total 19
Player 2 Cards Total 21
Turn 14
P1 = 7, P2 = 7
Player 1 Cards Total 20
Player 2 Cards Total 20
war
Turn 15
P1 = 4, P2 = 6
Player 1 Cards Total 15
Player 2 Cards Total 15
Turn 16
P1 = 4, P2 = 2
Player 1 Cards Total 14
Player 2 Cards Total 16
Turn 17
P1 = 13, P2 = 7
Player 1 Cards Total 15
Player 2 Cards Total 15
Turn 18
P1 = 9, P2 = 8
Player 1 Cards Total 16
Player 2 Cards Total 14
Turn 19
P1 = 6, P2 = 4
Player 1 Cards Total 17
Player 2 Cards Total 13
Turn 20
P1 = 13, P2 = 2
Player 1 Cards Total 18
Player 2 Cards Total 12
Turn 21
P1 = 3, P2 = 11
Player 1 Cards Total 19
Player 2 Cards Total 11
Turn 22

import random

def createPlayerDecks(shuffle_count=7):
card_face = [2,3,4,5,6,7,8,9,10,11,12,13,14]
deck = []

# Step 1: Create the deck 
for i in range(4):
    for card in card_face:
        deck.append(card)

# Step 2: Shuffle it a number of times
for i in range(shuffle_count):
    random.shuffle(deck)

# Step 3: Deal them out
players = [[],[]]
for i in range(len(deck)):
    players[i % 2].append(deck[i])

return players

players = createPlayerDecks()
print(players[0])
print(players[1])

def doWar(player_deck1, player_deck2, war_count):

player1_flip = None
player2_flip = None
risk_cards = []

if len(player_deck1) < war_count + 1:
    risk_cards.extend(player_deck1.copy())
    player_deck1.clear()
    player2_flip = player_deck2[-1]
elif len(player_deck2) < war_count + 1:
    risk_cards.extend(player_deck2.copy())
    player_deck2.clear()
    player1_flip = player_deck1[-1]


if len(player_deck1) and len(player_deck2):
   
    for i in range(war_count):
        risk_cards.append(player_deck1.pop())
        risk_cards.append(player_deck2.pop())

    '''
        Get the final card they will flip, also added to the risk cards
    '''
    player1_flip = player_deck1.pop()
    player2_flip = player_deck2.pop()
    risk_cards.extend([player1_flip, player2_flip])

while player1_flip == player2_flip:
    print("Continuation War")
    player1_flip, player2_flip, risk = doWar(player_deck1, player_deck2, war_count)
    risk_cards.extend(risk)


return player1_flip, player2_flip, risk_cards

def prependCards(deck, cards):
while len(cards):
deck.insert(0,cards.pop())

Create the player decks

players = createPlayerDecks(7)

Track number of turns

turns = 0
max_turns = 400

while len(players[0]) and len(players[1]):

turns += 1
print('Turn', turns)
if turns >= max_turns:
    break



p1Card = players[0].pop()
p2Card = players[1].pop()
current_cards = [p1Card, p2Card]
print("P1 = {}, P2 = {}".format(p1Card,p2Card))
print('Player 1 Cards Total', len(players[0]))
print('Player 2 Cards Total', len(players[1]))

if p1Card > p2Card:
    prependCards(players[0], current_cards)
elif p2Card > p1Card:
    prependCards(players[1], current_cards)
else:
    print("war")
    p1, p2, risk = doWar(players[0], players[1], 3)

winner_list = None
if not p1 or (p2 and p1 < p2):
winner_list = players[1]
elif not p2 or (p1 and p1 > p2):
winner_list = players[0]

prependCards(winner_list, risk)
prependCards(winner_list, current_cards)

winner = None
if len(players[0]) and len(players[1]):
if len(players[0]) > len(players[1]):
winner = "Player 1"
else:
winner = "Player 2"
else:
winner = "Player 1" if len(players[0]) > 0 else "Player 2"

print("{} wpm the game in {} turns".format(winner, turns))
print(len(players[0]), len(players[1]))

(WAR Card Game) Performing the act of War

For beginner programmers the logic in the card game War can be a little tricky. You need to think through the options that will occur in such a case. Putting it together for the first time can be difficult, and the goal is to not confuse you as a beginner programmer.

Given that, here is an implementation of the doWar() method you will need. Your goal is to add this function to your code then add the remaining game logic.

def doWar(player_deck1, player_deck2, war_count):
    '''
        doWar() function happens when the two players have identical cards. 

        PARAMETERS:
        player_deck1 : List of integers representing a deck of cards for Player 1
        player_deck2 : List of integers representing a deck of cards for Player 1
        war_count    : Integer representing the number of cards that are at risk. 
                       Total cards a player can lose are war_count + 1. The war_count 
                       at risk and one more to see who wins. 

        RETURNS:
        The return of the function is a 3 part tuple that can be accessed just like 
        a list, that is, with indexes. 

        result[0] - Flip card of the player1 after flipping the war_count cards, or
                    None if player 1 does not have enough cards and forfiets. 
        result[1] - Flip card of player 2 after flipping the war_count cards, or
                    None if player 2 does not have enough cards and forfiets. 
        result[2] - All of the cards, including the flip cards, that are at risk. 

        NOTES:
        If a player loses because of a forfiet then losers cards are all added to the risk cards.
        Whomever forfiets will have no return flip card, which is a special case to look for.

        If no forfiet happens it is up to the caller to detemine the winner with the return
        results of the call.  

        RULES:
            - If either player does not have enough cards to continue, they forfiet their
              remaining cards to the other (in the risk cards) and end up with an empty deck. 
            - Cards at risk are war_count + 1 for each player. Whomever wins takes all the cards
            - If another war occurs, the function is called recursively until there is a winner
              of the war or someone runs out of cards and forfiets.
    '''
    player1_flip = None
    player2_flip = None
    risk_cards = []

    '''
        Determine if either player does not have enough cards to continue. 
        
        If either player does not have enough cards, move all of that players cards
        onto the risk pile. Then, select one card for the "winner". 

        Upon return, it helps the caller figure out what happened. If either of the players
        flip cards are None, then that player forfieted and the other player wins. 
    '''
    if len(player_deck1) < war_count + 1:
        risk_cards.extend(player_deck1.copy())
        player_deck1.clear()
        player2_flip = player_deck2[-1]
    elif len(player_deck2) < war_count + 1:
        risk_cards.extend(player_deck2.copy())
        player_deck2.clear()
        player1_flip = player_deck1[-1]

    '''
        If there was no forfiet, collect war_count cards from each players
        deck and add them to the risk cards. 

        Next, take the next top card from each players deck as the flip card
        to determine who wins the war. These are also added to the risk cards. 
    '''
    if len(player_deck1) and len(player_deck2):
        '''
            Collect all the face down at risk cards
        '''
        for i in range(war_count):
            risk_cards.append(player_deck1.pop())
            risk_cards.append(player_deck2.pop())

        '''
            Get the final card they will flip, also added to the risk cards
        '''
        player1_flip = player_deck1.pop()
        player2_flip = player_deck2.pop()
        risk_cards.extend([player1_flip, player2_flip])

    '''
        If the result is that both players have flipped the same card value again, 
        we have another war. 

        In this case, call the function again (recursively) and get the return values. 
        The return risk cards are added to the risk_cards list because every card 
        played during a war will go to the winner. 
    '''
    while player1_flip == player2_flip:
        print("Continuation War")
        player1_flip, player2_flip, risk = doWar(player_deck1, player_deck2, war_count)
        risk_cards.extend(risk)

    '''
        Return the results of the war. 

        player1_flip : Player 1 flip card, if None this player forfieted
        player2_flip : Player 2 flip card, if None this player forfieted
        risk_cards   : All of the cards that were played during the war. These 
                       will go to the winner. 
    '''
    return player1_flip, player2_flip, risk_cards

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.