Skip to content

Instantly share code, notes, and snippets.

@priyankas0525
Created September 27, 2020 20:13
Show Gist options
  • Save priyankas0525/0d85d8bc5944b94a4a9871a14a136163 to your computer and use it in GitHub Desktop.
Save priyankas0525/0d85d8bc5944b94a4a9871a14a136163 to your computer and use it in GitHub Desktop.
Created on Skills Network Labs
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<center>\n",
" <img src=\"https://s3-api.us-geo.objectstorage.softlayer.net/cf-courses-data/CognitiveClass/Logos/organization_logo/organization_logo.png\" width=\"300\" alt=\"cognitiveclass.ai logo\" />\n",
"</center>\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"You will need the class Car for the next exercises. The class Car has four data attributes: make, model, colour and number of owners (owner_number). The method <code> car_info() </code> prints out the data attributes and the method <code>sell()</code> increments the number of owners. \n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"class Car(object):\n",
" def __init__(self,make,model,color):\n",
" self.make=make;\n",
" self.model=model;\n",
" self.color=color;\n",
" self.owner_number=0 \n",
" def car_info(self):\n",
" print(\"make: \",self.make)\n",
" print(\"model:\", self.model)\n",
" print(\"color:\",self.color)\n",
" print(\"number of owners:\",self.owner_number)\n",
" def sell(self):\n",
" self.owner_number=self.owner_number+1"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<h3> Create a Car object </h3>\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Create a <code> Car </code> object my_car with the given data attributes: \n"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"ename": "SyntaxError",
"evalue": "invalid syntax (<ipython-input-3-fbb139c5e5d5>, line 7)",
"output_type": "error",
"traceback": [
"\u001b[0;36m File \u001b[0;32m\"<ipython-input-3-fbb139c5e5d5>\"\u001b[0;36m, line \u001b[0;32m7\u001b[0m\n\u001b[0;31m def_init_(self,make,model,color):\u001b[0m\n\u001b[0m ^\u001b[0m\n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m invalid syntax\n"
]
}
],
"source": [
"make=\"BMW\"\n",
"model=\"M3\"\n",
"color=\"red\"\n",
"\n",
"\n",
"class Car(Object):\n",
" def_init_(self,make,model,color):\n",
" self.make=BMW;\n",
" self.model=M3;\n",
" self.color=red;\n",
" self.owner_number=0\n",
" def car_info(self):\n",
" print(\"make:\",self.make)\n",
" print(\"model:\", self.model)\n",
" print(\"color:\",self.color)\n",
" print(\"number of owners:\",self.owner_number)\n",
" def sell(self):\n",
" self.owne_number=self.owner_number+1"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<h3> Data Attributes </h3>\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Use the method car_info() to print out the data attributes\n"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"ename": "NameError",
"evalue": "name 'car_info' is not defined",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m<ipython-input-4-4e4570e2f534>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mcar_info\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[0;31mNameError\u001b[0m: name 'car_info' is not defined"
]
}
],
"source": [
"car_info()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<h3> Methods </h3>\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Call the method <code> sell() </code> in the loop, then call the method <code> car_info()</code> again \n"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"ename": "IndentationError",
"evalue": "expected an indented block (<ipython-input-5-5796f90efd09>, line 3)",
"output_type": "error",
"traceback": [
"\u001b[0;36m File \u001b[0;32m\"<ipython-input-5-5796f90efd09>\"\u001b[0;36m, line \u001b[0;32m3\u001b[0m\n\u001b[0;31m sell()\u001b[0m\n\u001b[0m ^\u001b[0m\n\u001b[0;31mIndentationError\u001b[0m\u001b[0;31m:\u001b[0m expected an indented block\n"
]
}
],
"source": [
"for i in range(5):\n",
" \n",
"sell()\n",
"Car_info()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<hr>\n",
"\n",
"<h3 align=\"center\"> © IBM Corporation 2020. All rights reserved. <h3/>\n"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python",
"language": "python",
"name": "conda-env-python-py"
},
"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.6.11"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment