0% found this document useful (0 votes)
110 views11 pages

CISC504A Vatsal Patel Assignment 4 O.ipynb

This document contains code snippets and output related to calculating temperature conversions between Fahrenheit and Celsius. The code takes user input for a temperature in Fahrenheit, converts it to Celsius using the formula C = (F - 32) * 5/9, and prints the results. It also handles exceptions if invalid input is provided. Additional snippets demonstrate taking command line arguments, calculating projectile motion equations, and determining the type of variables.

Uploaded by

Vatsal Patel
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
110 views11 pages

CISC504A Vatsal Patel Assignment 4 O.ipynb

This document contains code snippets and output related to calculating temperature conversions between Fahrenheit and Celsius. The code takes user input for a temperature in Fahrenheit, converts it to Celsius using the formula C = (F - 32) * 5/9, and prints the results. It also handles exceptions if invalid input is provided. Additional snippets demonstrate taking command line arguments, calculating projectile motion equations, and determining the type of variables.

Uploaded by

Vatsal Patel
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 11

{

"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": []
},
{
"cell_type": "code",
"execution_count": 33,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Enter a temperature in Fahrenheit:\n",
"3\n",
"3 Fahrenheit = -16.1111 Celsius\n"
]
}
],
"source": [
"#Youtube Link : https://youtu.be/zPEYl4e-UEQ\n",
"# Assignment 4\n",
"#Vatsal Patel (186383)\n",
"\n",
"#CISC 504-90- O-2020/Late Summer - Principles of Programming Langu\n",
"\n",
"#Instructor: Muazzam Ali\n",
"\n",
"F = raw_input(\"Enter a temperature in Fahrenheit:\\n\")\n",
"F = float(F)\n",
"C = 5 / 9. * (F - 32)\n",
"print \"%g Fahrenheit = %g Celsius\" % (F, C)\n",
"\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": []
},
{
"cell_type": "code",
"execution_count": 74,
"metadata": {},
"outputs": [
{
"ename": "ValueError",
"evalue": "could not convert string to float: -f",
"output_type": "error",
"traceback": [
"\u001b[1;31m----------------------------------------------------------------------
-----\u001b[0m",
"\u001b[1;31mValueError\u001b[0m Traceback
(most recent call last)",
"\u001b[1;32m<ipython-input-74-ec870f67d48e>\u001b[0m in
\u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[0;32m 1\u001b[0m
\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 2\u001b[0m
\u001b[1;32mimport\u001b[0m
\u001b[0msys\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---->
3\u001b[1;33m \u001b[0mF\u001b[0m \u001b[1;33m=\u001b[0m
\u001b[0mfloat\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0msys\u001b[0m\u001b[1;33m.\u00
1b[0m\u001b[0margv\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m1\u001b[0m\u001b[1;33m
]\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;3
2m 4\u001b[0m \u001b[0mF\u001b[0m \u001b[1;33m=\u001b[0m
\u001b[0mfloat\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mF\u001b[0m\u001b[1;33m)\u001b
[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 5\u001b[0m \u001b[0mC\u001b[0m
\u001b[1;33m=\u001b[0m \u001b[1;36m5\u001b[0m \u001b[1;33m/\u001b[0m
\u001b[1;36m9.\u001b[0m \u001b[1;33m*\u001b[0m
\u001b[1;33m(\u001b[0m\u001b[0mF\u001b[0m \u001b[1;33m-\u001b[0m
\u001b[1;36m32\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
"\u001b[1;31mValueError\u001b[0m: could not convert string to float: -f"
]
}
],
"source": [
"\n",
"import sys\n",
"F = float(sys.argv[1])\n",
"C = 5 / 9. * (F - 32)\n",
"\n",
"print (\"%g Fahrenheit = %g Celsius\" % (F, C)); \n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": []
},
{
"cell_type": "code",
"execution_count": 38,
"metadata": {},
"outputs": [
{
"ename": "ValueError",
"evalue": "could not convert string to float: -f",
"output_type": "error",
"traceback": [

"\u001b[1;31m----------------------------------------------------------------------
-----\u001b[0m",
"\u001b[1;31mValueError\u001b[0m Traceback
(most recent call last)",
"\u001b[1;32m<ipython-input-38-560628c3ca7c>\u001b[0m in
\u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[0;32m 1\u001b[0m
\u001b[1;32mimport\u001b[0m
\u001b[0msys\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m
2\u001b[0m
\u001b[1;32mtry\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001
b[1;32m----> 3\u001b[1;33m \u001b[0mF\u001b[0m \u001b[1;33m=\u001b[0m
\u001b[0mfloat\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0msys\u001b[0m\u001b[1;33m.\u00
1b[0m\u001b[0margv\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m1\u001b[0m\u001b[1;33m
]\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;3
2m 4\u001b[0m \u001b[1;32mexcept\u001b[0m
\u001b[0mIndexError\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\
u001b[0;32m 5\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n",
"\u001b[1;31mValueError\u001b[0m: could not convert string to float: -f"
]
}
],
"source": [
"import sys\n",
"try:\n",
" F = float(sys.argv[1])\n",
"except IndexError:\n",
"\n",
"\n",
"\n",
" print (\"You failed to provide a temperature in Fahrenheit as input on
the command line!\");\n",
"\n",
"\n",
"\n",
" sys.exit(1) # abort\n",
"\n",
"\n",
"\n",
" C = 5. / 9 * (F - 32)\n",
"\n",
"\n",
"\n",
" print (\"%g Fahrenheit = %g Celsius\" % (F, C));\n",
"\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": []
},
{
"cell_type": "code",
"execution_count": 26,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Enter input value:\n",
"35\n",
"35 is a Python <type 'int'> object\n"
]
}
],
"source": [
"val = eval(raw_input('Enter input value:\\n'))\n",
"print '%s is a Python %s object' % (val, type(val))\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": []
},
{
"cell_type": "code",
"execution_count": 27,
"metadata": {},
"outputs": [
{
"ename": "NameError",
"evalue": "name 'f' is not defined",
"output_type": "error",
"traceback": [

"\u001b[1;31m----------------------------------------------------------------------
-----\u001b[0m",
"\u001b[1;31mNameError\u001b[0m Traceback
(most recent call last)",
"\u001b[1;32m<ipython-input-27-3f4200b5a5d1>\u001b[0m in
\u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[0;32m 1\u001b[0m
\u001b[1;32mimport\u001b[0m
\u001b[0msys\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---->
2\u001b[1;33m \u001b[0mval\u001b[0m \u001b[1;33m=\u001b[0m
\u001b[0meval\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0msys\u001b[0m\u001b[1;33m.\u001
b[0m\u001b[0margv\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m1\u001b[0m\u001b[1;33m]
\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32
m 3\u001b[0m \u001b[1;32mprint\u001b[0m \u001b[1;34m'%s is a Python %s
object'\u001b[0m \u001b[1;33m%\u001b[0m
\u001b[1;33m(\u001b[0m\u001b[0mval\u001b[0m\u001b[1;33m,\u001b[0m
\u001b[0mtype\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mval\u001b[0m\u001b[1;33m)\u001
b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
"\u001b[1;32m<string>\u001b[0m in
\u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n",
"\u001b[1;31mNameError\u001b[0m: name 'f' is not defined"
]
}
],
"source": [
"import sys\n",
"val = eval(sys.argv[1])\n",
"print '%s is a Python %s object' % (val, type(val))\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": []
},
{
"cell_type": "code",
"execution_count": 24,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"v0 = ?\n",
"35\n",
"t = ?\n",
"23\n",
"-1789.745\n"
]
}
],
"source": [
"g = 9.81\n",
"v0 = float(raw_input('v0 = ?\\n'))\n",
"t = float(raw_input('t = ?\\n'))\n",
"y = v0 * t - 0.5 * g * t ** 2\n",
"print y\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": []
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"usage: ipykernel_launcher.py [-h] [--v0 v] [--t t]\n",
"ipykernel_launcher.py: error: unrecognized arguments: -f
C:\\Users\\vats3\\AppData\\Roaming\\jupyter\\runtime\\kernel-c1b0c5b0-37d1-4331-
afa9-e6c6def77fe9.json\n"
]
},
{
"ename": "SystemExit",
"evalue": "2",
"output_type": "error",
"traceback": [
"An exception has occurred, use %tb to see the full traceback.\n",
"\u001b[1;31mSystemExit\u001b[0m\u001b[1;31m:\u001b[0m 2\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"C:\\Users\\vats3\\Anaconda2\\lib\\site-
packages\\IPython\\core\\interactiveshell.py:2886: UserWarning: To exit: use
'exit', 'quit', or Ctrl-D.\n",
" warn(\"To exit: use 'exit', 'quit', or Ctrl-D.\", stacklevel=1)\n"
]
}
],
"source": [
"import argparse\n",
"g = 9.81\n",
"\n",
"parser = argparse.ArgumentParser()\n",
"\n",
"parser.add_argument('--v0', '--initial_velocity', type=float,\n",
" default=0.0, help='initial velocity', metavar='v')\n",
"parser.add_argument('--t', '--time', type=float, default=1.0,\n",
" help='time', metavar='t')\n",
"args = parser.parse_args()\n",
"\n",
"y = args.v0 * args.t - 0.5 * g * args.t ** 2\n",
"print y\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": []
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"v0 and t must be pure numbers\n"
]
},
{
"ename": "SystemExit",
"evalue": "1",
"output_type": "error",
"traceback": [
"An exception has occurred, use %tb to see the full traceback.\n",
"\u001b[1;31mSystemExit\u001b[0m\u001b[1;31m:\u001b[0m 1\n"
]
}
],
"source": [
"import sys\n",
"g = 9.81\n",
"try:\n",
" v0 = float(sys.argv[1])\n",
" t = float(sys.argv[2])\n",
"except IndexError:\n",
" print 'Both v0 and t must be supplied on the command line'\n",
" v0 = float(raw_input('v0 = ?\\n'))\n",
" t = float(raw_input('t = ?\\n'))\n",
"except ValueError:\n",
" print 'v0 and t must be pure numbers'\n",
" sys.exit(1)\n",
"\n",
"y = v0 * t - 0.5 * g * t ** 2\n",
"print y\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": []
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"v0 and t must be pure numbers\n"
]
},
{
"ename": "SystemExit",
"evalue": "1",
"output_type": "error",
"traceback": [
"An exception has occurred, use %tb to see the full traceback.\n",
"\u001b[1;31mSystemExit\u001b[0m\u001b[1;31m:\u001b[0m 1\n"
]
}
],
"source": [
"import sys\n",
"g = 9.81\n",
"try:\n",
" v0 = float(sys.argv[1])\n",
" t = float(sys.argv[2])\n",
"except IndexError:\n",
" print 'Both v0 and t must be supplied on the command line'\n",
" v0 = float(raw_input('v0 = ?\\n'))\n",
" t = float(raw_input('t = ?\\n'))\n",
"except ValueError:\n",
" print 'v0 and t must be pure numbers'\n",
" sys.exit(1)\n",
"\n",
"if t < 0 or t > 2 * v0 / g:\n",
" raise ValueError('t=%g is a non-physical value.'\n",
" 'must be between 0 and 2v0/g' % t)\n",
" sys.exit(1)\n",
"\n",
"y = v0 * t - 0.5 * g * t ** 2\n",
"print y\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": []
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"v0 and t must be pure numbers\n"
]
},
{
"ename": "SystemExit",
"evalue": "1",
"output_type": "error",
"traceback": [
"An exception has occurred, use %tb to see the full traceback.\n",
"\u001b[1;31mSystemExit\u001b[0m\u001b[1;31m:\u001b[0m 1\n"
]
}
],
"source": [
"import sys\n",
"g = 9.81\n",
"try:\n",
" v0 = float(sys.argv[1])\n",
" t = float(sys.argv[2])\n",
"except IndexError:\n",
" print 'Both v0 and t must be supplied on the command line'\n",
" v0 = float(raw_input('v0 = ?\\n'))\n",
" t = float(raw_input('t = ?\\n'))\n",
"except ValueError:\n",
" print 'v0 and t must be pure numbers'\n",
" sys.exit(1)\n",
"\n",
"if t < 0 or t > 2 * v0 / g:\n",
" raise ValueError('t=%g is a non-physical value.'\n",
" 'must be between 0 and 2v0/g' % t)\n",
" sys.exit(1)\n",
"\n",
"y = v0 * t - 0.5 * g * t ** 2\n",
"print y\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": []
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"year=1987\n",
"month=12\n",
"day=3\n",
"f{day}-{month}-{year} was a Thursday\n"
]
}
],
"source": [
"from calendar import weekday, day_name\n",
"year = int(input(\"year=\"))\n",
"month = int(input(\"month=\"))\n",
"day = int(input(\"day=\"))\n",
"print(\"f{day}-{month}-{year} was a %s\" % day_name[weekday(year, month,
day)])\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": []
},
{
"cell_type": "code",
"execution_count": 21,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Enter file name: DataforWord\n"
]
},
{
"ename": "NameError",
"evalue": "name 'DataforWord' is not defined",
"output_type": "error",
"traceback": [

"\u001b[1;31m----------------------------------------------------------------------
-----\u001b[0m",
"\u001b[1;31mNameError\u001b[0m Traceback
(most recent call last)",
"\u001b[1;32m<ipython-input-21-66b32477543e>\u001b[0m in
\u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m
\u001b[0mfname\u001b[0m \u001b[1;33m=\u001b[0m
\u001b[0minput\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m\"Enter file
name: \"\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u0
01b[0;32m 2\u001b[0m
\u001b[0mword\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0minput\u001b[0m\u001b[1;33m(\u0
01b[0m\u001b[1;34m\"Enter word to be
searched:\"\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;
32m 3\u001b[0m \u001b[0mk\u001b[0m \u001b[1;33m=\u001b[0m
\u001b[1;36m0\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m
4\u001b[0m \u001b[1;32mwith\u001b[0m
\u001b[0mopen\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mfname\u001b[0m\u001b[1;33m,\u0
01b[0m \u001b[1;34m'r'\u001b[0m\u001b[1;33m)\u001b[0m \u001b[1;32mas\u001b[0m
\u001b[0mf\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;3
2m 5\u001b[0m \u001b[1;32mfor\u001b[0m \u001b[0mline\u001b[0m
\u001b[1;32min\u001b[0m
\u001b[0mf\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
"\u001b[1;32mC:\\Users\\vats3\\Anaconda2\\lib\\site-
packages\\ipykernel\\ipkernel.pyc\u001b[0m in
\u001b[0;36m<lambda>\u001b[1;34m(prompt)\u001b[0m\n\u001b[0;32m 174\u001b[0m
\u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_sys_eval_input\u001b[0m
\u001b[1;33m=\u001b[0m
\u001b[0mbuiltin_mod\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0minput\u001b[0m\u001b[1;
33m\u001b[0m\u001b[0m\n\u001b[0;32m 175\u001b[0m
\u001b[0mbuiltin_mod\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mraw_input\u001b[0m
\u001b[1;33m=\u001b[0m
\u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mraw_input\u001b[0m\u001b[1;33m
\u001b[0m\u001b[0m\n\u001b[1;32m--> 176\u001b[1;33m
\u001b[0mbuiltin_mod\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0minput\u001b[0m
\u001b[1;33m=\u001b[0m \u001b[1;32mlambda\u001b[0m
\u001b[0mprompt\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;34m''\u001b[0m\u001b[1;33m:\
u001b[0m
\u001b[0meval\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m.\u00
1b[0m\u001b[0mraw_input\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mprompt\u001b[0m\u001
b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u0
01b[0;32m 177\u001b[0m
\u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_save_getpass\u001b[0m
\u001b[1;33m=\u001b[0m
\u001b[0mgetpass\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mgetpass\u001b[0m\u001b[1;33
m\u001b[0m\u001b[0m\n\u001b[0;32m 178\u001b[0m
\u001b[0mgetpass\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mgetpass\u001b[0m
\u001b[1;33m=\u001b[0m
\u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mgetpass\u001b[0m\u001b[1;33m\u
001b[0m\u001b[0m\n",
"\u001b[1;32mC:\\Users\\vats3\\Anaconda2\\lib\\site-
packages\\ipykernel\\ipkernel.pyc\u001b[0m in
\u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n",
"\u001b[1;31mNameError\u001b[0m: name 'DataforWord' is not defined"
]
}
],
"source": [
"fname = input(\"Enter file name: \")\n",
"word=input(\"Enter word to be searched:\")\n",
"k = 0\n",
"with open(fname, 'r') as f:\n",
" for line in f:\n",
" words = line.split()\n",
" for i in words:\n",
" if(i==word):\n",
" k=k+1\n",
"print(\"Number of Specific words:\")\n",
"print(k)\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 2",
"language": "python",
"name": "python2"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 2
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython2",
"version": "2.7.16"
}
},
"nbformat": 4,
"nbformat_minor": 2
}

You might also like