Compsci 6/101, Fall 2011, Data Interaction HOWTO

This page provides more specific guidelines about the two separate programs you are to write: a hangman game and a stock portfolio simulator. As well as some general advice on dealing with user input and writing functions.

User Input

The sample interactions given below are representative and should be generally followed, but you can be more creative in your prompts and outputs.

Since these programs are driven by input from the user (should it continue, what letter is guessed, what file to read, etc.), your program should try to be robust in the face of bad input from the user. For example, the user may make the following errors (by accident of course):

Note, this is a representative list, not an exhaustive one. your programs will get more credit for detecting (and informing the user) of as many kinds of bad input as possible and giving the user another chance to enter reasonable data.

Handling Numbers

Remember that Python's raw_input() function returns only a String. Since it is an error in Python to convert a String containing letters into an int, the String object has two ways to help determine if the user's input is indeed a number:

Neither of these is completely robust (i.e., handles both positive or negative numbers, or real valued numbers with a decimal point), so you will have to use them as part of a larger function to determine if a String is indeed a valid number.

Handling Filenames

In both programs, you will need to ask the user to choose which file to use (in Hangman, that file represents the possible words used for the secret word to guess; in Stock Trader, that file represents the company whose stock values you want to analyze). In general, asking a user to enter a filename is both error prone (due to spelling errors) or uncomfortable (because the data is somewhere else on your hard drive, like "../data/stocks" or "C:\data\hangman"). Thus you should provide some means of helping the user choose the initial data file, such as:

These examples are not robust (as described above) but they should give you an idea of how to proceed. The only wrong option is to prompt for a file name directly.

Guidelines for Writing Functions

Your program must consist of functions and function calls. There can be no global variables in your program.

As part of developing these programs, develop general functions for working with the data files, user input and output, that can be reused across both programs. These general functions should be written in a separate module and imported into each of the programs you write. To help you start thinking about what should be a general function, consider these guidelines that your functions should

Note, all your sub-functions should be able to be called directly, i.e., without asking the user for input, so that your programs are capable of having that information provided other than from the Console, i.e., from a GUI, a set as constants, or called directly with test values. In short, you should limit your calls to raw_input() to one or two top-level functions that drive the the program.

Part I: Hangman

Write a program to play a console-based, word-oriented game of hangman. The user should be allowed to specify the number of letters in the word and the number of misses until the game is lost (see the sample runs below for details). However, if the user enters 0 for the length of the word then it should any possible word from its dictionary file. The dictionary should be read one line at a time: if that line contains multiple words, then they should all be included as part of the secret word. However, you should not expect people to guess spaces, so those should be clearly marked as spaces in your output.

Sample Run

Here's a sample run of one game of Hangman (where the secret word dictionary has already been chosen). You do not need to follow the format exactly, but you should include with each turn the player takes the following information:

In the run below the user input is in italics, the other text is printed by the program.

# letters in word:  8
# guesses to hanging:  7
_ _ _ _ _ _ _ _
misses left:  7
guesses so far:  
guess letter:  e
no e
_ _ _ _ _ _ _ _
misses left:  6
guesses so far:  e 
guess letter:  a
no a
_ _ _ _ _ _ _ _
misses left:  5
guesses so far:  a e 
guess letter:  o
no o
_ _ _ _ _ _ _ _
misses left:  4
guesses so far:  a e o 
guess letter:  u
no u
_ _ _ _ _ _ _ _
misses left:  3
guesses so far:  a u e o 
guess letter:  i
_ _ _ i _ _ i _
misses left:  3
guesses so far:  a u e o 
guess letter:  s
no s
_ _ _ i _ _ i _
misses left:  2
guesses so far:  a e o s u 
guess letter:  t
no t
_ _ _ i _ _ i _
misses left:  1
guesses so far:  a e o s u t 
guess letter:  r
_ _ r i _ _ i _
misses left:  1
guesses so far:  a e o s u t 
guess letter:  n
no n
you are hung :-(, secret word is cyrillic
Do you want to play another game (yes or no)? no

Part II: Building a Stock Portfolio

As you saw in Lab this week, free historical stock market data for individual companies is available from Yahoo! Finance; for this program, we will be using the same data files as Lab. The fields within each line of data in these files are labelled as the first line of the data file.

Unlike the principled approach discussed in lab, this investing strategy consists of two steps:

  1. Detect a Pattern: if the closing price of the stock goes up at least some number of consecutive days, the next day it goes down is a good time to sell the stock. Analogously, if the stock goes down at least some number of consecutive days, the next day it goes up is a good time to buy the stock.
  2. Invest: Assume that you will convert all of your cash to stock when you detect a pattern that signals you to buy, and that you will convert all of your stock to cash when you detect a pattern that signals you to sell. For each time day, print out the price, cash, shares owned, and portfolio value. The value of your portfolio is cash plus the number of shares multiplied by the price per share. (For simplicity, assume that you can buy fractional amounts of stock, and there are no transaction fees.)

Your program should determine how much money you would have won or lost using this rule over the given period of time you have data for the chosen stock.

You should allow the user to pick a company to invest in, an initial amount of money to invest, and a minimum number of consecutive days to activate your actions.

Here's a sample run of one investment (where the company has already been chosen) for data that covers three weeks.

In the run below the user input is in italics, the other text is printed by the program.

starting amount of money: 10000
# days in trend: 3
day   price     cash       shares    value
-----------------------------------------------
 1    26.375   10000.00       0.00   10000.00
 2    25.500   10000.00       0.00   10000.00
 3    25.125   10000.00       0.00   10000.00
 4    25.000   10000.00       0.00   10000.00
 5    25.250       0.00     396.04   10000.00
 6    27.125       0.00     396.04   10742.57
 7    28.250       0.00     396.04   11188.12
 8    26.000   10297.03       0.00   10297.03
 9    25.500   10297.03       0.00   10297.03
10    25.000   10297.03       0.00   10297.03
11    25.125       0.00     409.83   10297.03
12    25.250       0.00     409.83   10348.26 
13    26.375       0.00     409.83   10809.32
14    25.500   10450.72       0.00   10450.72
15    25.500   10450.72       0.00   10450.72
Do you want to try another simulation (yes or no)? no