2nd Semester Intro to Computer Science

What you will learn

Reading

Do Now

In your Console

Type the following code

import random
# inputs:  x (int), y (int)
# outputs: int
# 50% returns sum of x and y, 50% returns product of x and y

def mystery_function(x, y):
  random_number = random.randint(0,1)
  if random_number > 0:
    z = x + y
  else:
    z = x * y
  return z
mystery_function(1, 2)

In your Notebook

Answer the following questions

  1. What happens when your run this code? How do you know what the result was?
  2. Keeping the function the same, rewrite the code to print out the value that the function returns.

Lab

Create a program that lets a user play a simplified version of the card game ‘War’. In this version, the users will share a single deck of cards and cards will not be added back to the deck after they have been played.

Video Explanation of the Card Game War

Card Game: War

Your game should

# name: player_turn
# purpose: takes in a player name and draws/removes a card from the deck, prints "user drew card x", and returns the value
# Arguments: player_name as string, deck as list
# returns: integer

Sample Output

Player 1’s name: Pat
Player 2’s name: Sam

Pat drew card 8
Sam drew card 9
Sam has high card
Pat: 0
Sam: 2

Pat drew card 9
Sam drew card 8
Pat has high card
Pat: 2
Sam: 2

Pat drew card 7
Sam drew card 7
War
Pat: 2
Sam: 2

Pat drew card 5
Sam drew card 6
Sam has high card
Sam wins war of 4 cards
Pat: 2
Sam: 6

Pat drew card 13
Sam drew card 14
Sam has high card
Pat: 2
Sam: 8

Pat drew card 6
Sam drew card 12
Sam has high card
Pat: 2
Sam: 10

Pat drew card 4
Sam drew card 8
Sam has high card
Pat: 2
Sam: 12

Pat drew card 12
Sam drew card 2
Pat has high card
Pat: 4 Sam: 12

Pat drew card 7
Sam drew card 13
Sam has high card
Pat: 4
Sam: 14

Pat drew card 10
Sam drew card 6
Pat has high card
Pat: 6
Sam: 14

Pat drew card 9
Sam drew card 7
Pat has high card Pat: 8
Sam: 14

Pat drew card 4
Sam drew card 13
Sam has high card
Pat: 8
Sam: 16

Pat drew card 3
Sam drew card 3
War
Pat: 8
Sam: 16

Pat drew card 11
Sam drew card 3
Pat has high card
Pat wins war of 4 cards
Pat: 12
Sam: 16

Pat drew card 4
Sam drew card 10
Sam has high card Pat: 12
Sam: 18

Pat drew card 12
Sam drew card 11
Pat has high card
Pat: 14
Sam: 18

Pat drew card 4
Sam drew card 11
Sam has high card Pat: 14
Sam: 20

Pat drew card 8
Sam drew card 5
Pat has high card
Pat: 16
Sam: 20

Pat drew card 12
Sam drew card 9
Pat has high card
Pat: 18
Sam: 20

Pat drew card 5
Sam drew card 6
Sam has high card
Pat: 18
Sam: 22

Pat drew card 10
Sam drew card 13
Sam has high card
Pat: 18
Sam: 24

Pat drew card 2
Sam drew card 2
War
Pat: 18
Sam: 24

Pat drew card 14
Sam drew card 14
War
Pat: 18
Sam: 24

Pat drew card 2
Sam drew card 5
Sam has high card
Sam wins war of 6 cards
Pat: 18
Sam: 30

Pat drew card 11
Sam drew card 14
Sam has high card
Pat: 18
Sam: 32

Pat drew card 10
Sam drew card 3
Pat has high card
Pat: 20
Sam: 32

Final Score
Pat: 20
Sam: 32
Winner: Sam

Deck Shuffling

While seemingly simple– shuffling a deck is a somewhat complicated problem. Luckily, Python’s random library has a built in shuffle algorithm. Feel free to read the documentation, but we have provided a simple wrapper function that will return to you a shuffled deck of cards.


import random

# name: shuffled_deck
# purpose: will return a shuffled deck to the user
# input:
# returns: a list representing a shuffled deck
def shuffled_deck():
  basic_deck = list(range(2, 15)) * 4
  random.shuffle(basic_deck)
  return basic_deck

Bonus

Instead of closing the program when the deck is empty, create a way for the user to play again.

Check your understanding

Pick an answer for each question. You can try again until you get it right.

1. A function ends with return z. You call it in a script as mystery(1, 2) on its own line, with no print. What is displayed?

  • Nothing, the value is returned but not printed
  • The value of z
  • The word return
  • An error

Returning hands a value back to the caller; only print shows it to the user.

2. What value does a function give back if it has no return statement?

  • 0
  • An empty string
  • None
  • An error is always raised

Functions without a return give back None.

3. Given def double(n): return n * 2, what does print(double(4) + 1) display?

  • 5
  • 9
  • 81
  • Nothing

double(4) returns 8, and then 1 is added to it.

Downloads