MONT 112G, Fall 2011

    Home | | Course Schedule | | Assignments | | Lecture Notes

    MONT 112G Homework 7

    How Much Can You Buy for a Dollar?

    Due Wednesday, November 2, in class

    Introduction
    In this assignment, you will write a program to aid a player in a text game who wishes to know which items for sale are within his or her price range. The player can type in the amount he or she wishes to spend and the program will print out the name and price of each item with a price that is less than or equal to that amount. The goal of this assignment is to increase your familiarity with "for loops" and lists in python.

    Program Description
    The program will begin by creating two empty lists. One list, named item, will contain strings that are the descriptions of the items for sale. The second list, named price, will contain real numbers that are the prices of those items. The lists will be set up so that the price of the first element in the item list will be the first element of the price list. In other words, the price of item[0] will be price[0], the price of item[1] will be price[1] and so on.

    The program will start by having the user enter the items and prices into the two lists. The program should prompt the user for the number of items, and then read in each item and its price and add these to the appropriate lists (using the append( ) function). The program should then prompt the user for an amount that they wish to spend. Once the user has entered the amount, the program will print out the name and price of each item whose price is less than or equal to that amount.

    Project Specifications
    Place all your code in a file named <username>_price.py, where refers to your own username. For example, Professor Royden's file would be named croyden_price.py.

    1. Create variables for the two lists and assign an empty list to each of them.
    2. Prompt the user to enter the number of items in the inventory.
    3. Use a for loop to enter each item and its price and add them to the appropriate lists.
    4. Prompt the user to enter the amount he or she wishes to spend and read it in.
    5. Based on the amount, the computer should list the name and price of every item whose price is less than or equal to the amount entered by the user. You should use a for loop to accomplish this.
    6. Write your code to assure that the text that appears on the monitor looks like that displayed in the section below.

    Specifications for Output.
    Directly below, you can see an example of the display that will appear on the monitor screen for several sample runs of the program. Underlined items indicate user input.

    Sample Run 1:

    How many items are in the inventory? 4
    
    Please enter the item name: potion
    Please enter the price of the item: 2.75
    Please enter the item name: lantern
    Please enter the price of the item: 1.34
    Please enter the item name: wand
    Please enter the price of the item: 7.89
    Please enter the item name: armor
    Please enter the price of the item: 15.83
    
    How much would you like to spend? 5.00
    
    The following can be bought for $5.00:
    Item: potion, Price: $2.75
    Item: lantern, Price: $1.34
    

    Sample Run 2:

    How many items are in the inventory? 3
    
    Please enter the item name: Gold bars
    Please enter the price of the item: 2000.32
    Please enter the item name: sword
    Please enter the price of the item: 10.56
    Please enter the item name: magic staff
    Please enter the price of the item: 15.99
    
    How much would you like to spend? 50
    
    The following can be bought for $50.00:
    Item: sword, Price: $10.56
    Item: magic staff, Price: $15.99
    

    To Submit Your Finished Project:
    1. Hand in a hard copy of the file <username>_price.py. Hand this to your instructor in class on the project's due date.

    3. Hand in a printout of 2 sample runs like those shown above. Hand this to your instructor in class on the project's due date.

    4. Print your name at the top of the cover page and staple it to the top of your hard copy.

    5. In addition to the hard copy listed above, email your <username>_price.py file to me at croyden@mathcs.holycross.edu

    Get started early and have fun!