Introduction to Python

davidbpython.com




Projects, Session 1



Please note that our first homework is due the night before our second class session. It will not be assigned until after the first class. PLEASE REMEMBER:

  1. re-read the assignment before submitting
  2. go through the checklist including the tests
  3. make sure your notations are as specified in the homework instructions

All requirements are detailed in the homework instructions document.

Careless omissions will result in reductions to your solution grade.

 
1.1 Notes typing assignment -- please write out this week's notes:

  1. Looking in the folder you downloaded and unzipped, in the project directory for this week (this is the folder with the name starting with session_01 where we found the exercises for the week), locate an image named transcription_1.png.
  2. Double-click or open the image and simply type what you see displayed there.
  3. Submit your transcription (what you have typed) as the first homework assignment.
  4. This should not be in a Python program - use a simple text file, or type directly into homework submission window.

 
1.2 Tip calculator: write a restaurant bill calculator that takes three inputs (taken from the user with input(): a restaurant bill total charge, the number of people who will be splitting the bill, and the desired tip in percent of the total bill.
Sample program run:
Please enter the total bill amount:  120
Please enter the number in your party:  5
Please enter the desired tip percentage (for example,
"20" for 20%):  15
A 15.0% tip ($18.0) was added to the bill, for a total of $138.0.
With 5 in your party, each person must pay $27.6
Sample program run:
Please enter the total bill amount:  100
Please enter the number in your party:  2
Please enter the desired tip percentage (for example,
"20" for 20%):  20
A 20.0% tip ($20.0) was added to the bill, for a total of $120.0
With 2 in your party, each person must pay $60.0

Please note you do not need to submit multiple copies of your code. These are sample runs of your code. Please submit just one copy of your code, but run your code as shown in the examples.
HOMEWORK CHECKLIST: all points are required


    testing: you have run the program with the sample inputs as shown and are seeing the output exactly as shown (contact me if your output is different and you're unable to adjust to match)

    input values come from the input() function

    commas are not used to separate items in print statements (use concatenation). Commas are OK inside any string.

    code runs (and outputs) as shown in the examples

    there are no extraneous comments or "testing" code lines

    there are no unnecessary type conversions (for example str('hello'))

    program runs as shown in the assignment, or if it doesn't, a comment is placed at the top explaining what error or bad output has occurred (it is fine to turn in an incomplete solution if you have a question or would like to discuss ways to improve)

 
1.3 Exponientation with tidy border: start with the following code, which takes input from the user twice:
var_1 = input('Please enter an integer:  ')
var_2 = input('Please enter another integer:  ')

  • Raise the first number to the power of the second number (you will have to convert them to numbers to do the math).
  • Take the resulting number and determine its string length with len() (you will have to convert it to a string to get its string length).
  • Print a line with a ======== border exactly the length of the resulting number (use the * operator to repeat the string '=').
  • On the next line, print the number.
  • Then on a third line print a ======== border the same length as the first.
  • Border lengths must not be "hard coded"! They must reflect the size of the integer.

  • Sample program runs:
     please enter an integer:  3
     please enter another integer:  14
     =======
     4782969
     =======
     please enter an integer:  3
     please enter another integer:  5
     ===
     243
     ===

    Please note you do not need to submit multiple copies of your code. These are sample runs of your code. Please submit just one copy of your code, but run your code as shown in the examples.
    HOMEWORK CHECKLIST: all points are required


        testing: you have run the program with the sample inputs as shown and are seeing the output exactly as shown (contact me if your output is different and you're unable to adjust to match)

        solution is to homework, not exercises (exercises are numbered Ex.1.1, Ex.1.2, etc.)

        there is only one solution in each submission (this one is for Homework 1.1)

        there are no extraneous comments or "testing" code lines

        there are no unnecessary type conversions (for example str('hello'))

        program runs as shown in the assignment, or if it doesn't, a comment is placed at the top explaining what error or bad output has occurred (it is fine to turn in an incomplete solution if you have a question or would like to discuss ways to improve)

     

    EXTRA CREDIT / SUPPLEMENTARY EXERCISES These assignments are intended to give you additional practice if you have the time and inclination. I will look at any submissions from this unit, although I may not have as much time to offer detailed inline comments as I do with the required assignments.

     
    1.4 (extra credit / supplementary) Price Per Unit Comparison: write a program that takes four inputs: the price of a product, the unit size of that product (for example, "5" for five ounces, or "2.5" for 2.5 litres), the price of a second product, and the unit size of that product (using the same units). The program will then calculate the price per unit of each product and show which product is the better value on that basis.
    Sample program run:
    please enter the unit size of Product 1:  100
    please enter the price of Product 1:  10
    please enter the unit size of Product 2:  50
    please enter the price of Product 2:  7.50
    Product 1 costs $0.1 per unit
    Product 2 costs $0.15 per unit
    Product 1 is 66.67% the per-unit cost of Product 2
     
    1.5 (extra credit) Share Calculator: this program is just a practical demonstration of how easily Python can pull in data from the web for use in your programs. It will only work if your computer is connected to the internet (and there are no firewall or other systems in place that might prevent a network connection from your computer).

    Calculate the number of shares of a given stock a given amount of money can buy. Take user input for a stock symbol (for example, "AAPL" (Apple Inc.), "GOOG" (Google Inc.), "DOW" (Dow Chemical Co.) and a cash dollar amount, and calculate how many shares could be bought for the cash. The program uses a pre-written function (get_price(), supplied below) to retrieve the stock's current price from the internet to use in the calculation. Please note that in order to run this code you will need to install a 3rd party module: requests. See discussion for instructions.

    Starter Code:
    def get_price(symbol):
        import requests          # may need to be installed: see above
    
        apikey = 'alphavantage'
    
        url = ( 'https://www.alphavantage.co/query?'
                'function=TIME_SERIES_INTRADAY'
               f'&symbol={symbol}&interval=1min&apikey={apikey}'
                '&datatype=csv')
    
        response = requests.get(url)
        text = response.text
    
        try:
            quote = text.splitlines()[1].split(',')[4]
        except IndexError:
            if 'demo purposes' in text:
                exit('\n*** NOTE ***  This demo URL can only be '
                     'used with MSFT.  To obtain live data on '
                     'other symbols, you must edit your program '
                     'to change "apikey = " variable in this '
                     'function to a user key you can obtain from '
                     'the "alphavantage" service by signing up for '
                     'a free account.  See error message below: '
                     ' \n\n' + text)
    
            exit('error parsing response.  Response:  ' + text)
    
        return quote
    
    aa = input('please input the stock symbol you would '
                   'like to buy:  ')
    
    bb = input('please input the cash you have to invest:  ')
    
    cc = get_price(aa)      # returns a string price
    
    ## your code goes here - calculate the # of shares
    ## that can be bought with the user's cash
    
    Sample program run:
    please input the stock symbol you would like to buy:  MSFT
    please input the cash you have to invest:  10000
    with $10000 you can buy 121 shares of MSFT at $82.40/share
     
    [pr]