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:
|
||||||||||||||||||||||||||||||||||||||||||
All requirements are detailed in the homework instructions document. |
||||||||||||||||||||||||||||||||||||||||||
1.1 | Notes typing assignment -- please write out this week's notes: | |||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||
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.
|
||||||||||||||||||||||||||||||||||||||||||
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: ')
|
||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||
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.
|
||||||||||||||||||||||||||||||||||||||||||
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 |
||||||||||||||||||||||||||||||||||||||||||