IT 116: Introduction to Scripting
Homework 11

Due

Sunday, April 27th at 11:59 PM

What You Need to Do

Setup On Your Machine

Specification

Functions

create_deck

	create an empty list
	for suit in suits:
	   for value in values:
	      set card to a string consisting of the value, "of" and suit
	      append card to the list
	return the list

shuffle

	create an empty list
	while there are still entries in deck
	   set index to a randomly chosen index in deck
	   remove 
	   get the card deck[index] and remove it
	   append this card to the list
	return the list

deal_hand

	create an empty list
		for index in deck 5 times:
		   get the card deck[index] and remove it
		   append this card to the list
		return the list

Test Code

Output

Suggestions

  1. Import the random module.
    Copy only the function headers for the three functions listed above into your script file.
    Under each header, write pass.
    Make sure this statement is indented.
    Run the script.
    Fix any errors you find.
  2. Remove the pass statement from create_deck.
    Write a statement that creates the empty list list. Print the parameters suits and values.
    Copy the test code to the bottom of the script.
    Comment out the last three lines of the test script.
    Run the script.
    Fix any errors you find.
  3. Remove the print statement. Write a for loop that iterates through the list suits.
    The loop variable should be named suit.
    Inside the loop print suit.
    Run the script.
    Fix any errors you find.
  4. Remove the print statement.
    Inside the for loop write another for loop that iterates through values.
    The loop variable should be named value.
    Inside this second loop print value. Run the script.
    Fix any errors you find.
  5. Remove the print statement.
    Inside the 2nd for loop you will need to create the string card.
    This string will consist of value, followed by " of ", followed by suit.
    Print(card).
    Run the script.
    Fix any errors you find.
  6. Remove the print statement.
    Append card to list.
    Outside both loops, print list.
    Run the script.
    Fix any errors you find.
  7. Remove the print statement.
    Replace it with a statement that returns list.
    Remove the pass statement fromshuffle.
    Replace it with a statement that creates the empty list shuffled_deck.
    Print the parameter deck.
    Remove the # from the first commented out test code line.
    Run the script.
    Fix any errors you find.
  8. Let me explain what needs to happen next.
    The parameter deck is the ordered list of cards created by create_deck.
    The function will create a new list, shuffled.
    We need to randomly remove all the cards from deck and add them to the list shuffled,.
    This will be done inside a while loop that runs as long as their are cards in deck.
    Inside the loop, pop will be called to remove a card from deck.
    pop returns the card, which will be added to shuffled.
    If we call pop without an argument, it will return the last card in deck.
    But that will not be a randomly chosen card.
    We need to create a randomly chosen index to give to pop.
    You need to use random.randrange to do this.
    The argument to randrange should be len(deck).
    Outside the loop. the function shoul return shuffled.
  9. Remove the print statement from shuffle.
    Create the empty list shuffled.
    Write a while where loop is the condition is the length of the list deck.
    In order for this list to work, we will need to remove an element with each pass through the loop.
    Inside the loop create the randomly chosen index index by calling randrange on the length of deck.
    Use index as the argument in a call to pop and assign the returned value to card.
    Print cars. Run the script.
    Fix any errors you find.
  10. Remove the print statement.
    Replace it with a statement that adds card to shuffled.
    Outside the while loop, print shuffled.
    Run the script.
    Fix any errors you find.
  11. Remove the print statement.
    . Replace it with a statement that returns shuffled.
    Inside deal_hand remove the pass statement.
    Replace it with a statement that prints the parameter deck.
    Uncomment the next line in the test code. Run the script.
    Fix any errors you find.
  12. Remove the print statement.
    Replace it with a statement that creates the empty list hand.
    Write a for loop that runs 5 times.
    Inside the for loop set the variable card to the value you get by calling pop on deck.
    Still inside the loop, print card.
    Run the script.
    Fix any errors you find.
  13. Remove the print statement.
    Replace it with a statement that appends card to hand.
    Outside the loop print hand. Run the script.
    Fix any errors you find.
  14. Remove the print statement.
    Replace it with a statement that returns hand.
    Uncomment the last line in the text code.
    Run the script.
    Fix any errors you find.

Testing on Your Machine

Copy the Script to Unix

Testing the Script on Unix (Optional)

Copyright © 2020 Glenn Hoffman. All rights reserved. May not be reproduced without permission.