Lab 11  More Loops

Lab Index  Course Home Page

Task 1  String to list of integers

Write code to convert a string containing integers into a list of numeric, not string, integers.  For instance the string "2 4 10 15 3 8" would produce [2, 4, 10, 15, 3, 8].   Hint split the string and convert each string representation of a number to its numberic value, and add to a new list.

Task 2  String to list of Points

Extend or modify Task 1 so a string of integers is used to produce a list of graphics Points.  You may assume there are an even quantity of integers.  Place the code in the skeleton below to draw a polygon using the list of Points. 

from graphics import *
win = GraphWin("Polygon Plot", 200, 200)
intStr = "10 20 90 25 190 100  100 180 40 160"
# write you code to produce a list called vertices

poly = Polygon(vertices)
poly.draw(win)
win.getMouse()
win.close()

Task 3  File to Polygons

Modify the code in Task 2 to read all the lines of a file, where each line contains an even number of integers, and draws a Polygon for each line.  Here is a sample file coord.txt.

Task 4  Deal Hands of Cards

Prompt the user for a number of hands and a number of cards per hand, and print the hands out, one per line.   For example, if the request is for 3 hands of 5 cards each, you might print
QH 5S 9S 2C 9H
AS 3D 8H 6S KD
6D JD 6S JS JH

I do not care which places in the deck you take each card from, as long as each is different.  Also I do not care if the deck ends up with fewer cards than it started with or if it remains unchanged.  Two approaches are to keep track of individual indices or to use slices. 

You may use the code from class that produces a shuffled deck, either from the link or from below:

import random

def main():
    suits = ['C', 'D', 'H', 'S']
    values = ['A', '2', '3', '4', '5', '6', '7', '8', '9', 'T', 'J', 'Q', 'K']

    deck = []
    for val in values:
        for suit in suits:
            deck.append(val + suit)
    random.shuffle(deck)
    # write code here

main()
 

Show a TA Tasks 3 and 4.


Lab Index  Course Home Page