Skip to content

Instantly share code, notes, and snippets.

@natashawatkins
Created September 1, 2020 21:18
Show Gist options
  • Save natashawatkins/ec2eec34e8094073fc47d81c83fd520a to your computer and use it in GitHub Desktop.
Save natashawatkins/ec2eec34e8094073fc47d81c83fd520a to your computer and use it in GitHub Desktop.
will_tutorial.ipynb
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"class Consumer:\n",
"\n",
" def __init__(self, w):\n",
" \"Initialize consumer with w dollars of wealth\"\n",
" self.wealth = w\n",
"\n",
" def earn(self, y):\n",
" \"The consumer earns y dollars\"\n",
" self.wealth += y\n",
"\n",
" def spend(self, x):\n",
" \"The consumer spends x dollars if feasible\"\n",
" new_wealth = self.wealth - x\n",
" if new_wealth < 0:\n",
" print(\"Insufficent funds\")\n",
" else:\n",
" self.wealth = new_wealth"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"will = Consumer(100) # Starts with 100 wealth"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"<__main__.Consumer at 0x110e6c690>"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"will"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"100"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"will.wealth"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [],
"source": [
"will.earn(50) # earns 50"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"150"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"will.wealth"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.4"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment