{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "*This notebook contains material for CBE 20258 Numerical and Statistical Analysis taught at the University of Notre Dame. (c) Professors Alexander Dowling, Ryan McClarren, and Yamil Colón. This collection of notebooks [cbe-xx258](https://ndcbe.github.io/cbe-xx258) is available [on Github](https://github.com/ndcbe/cbe-xx258).*\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "< [1.2 Learning Python Basics](https://ndcbe.github.io/cbe-xx258/01.02-Variables.html) | [Contents](toc.html) | [1.4 Functions, Scoping, and Other Fun Stuff](https://ndcbe.github.io/cbe-xx258/01.04-Functions-scoping.html) >
"
]
},
{
"cell_type": "markdown",
"metadata": {
"colab_type": "text",
"id": "lcbP21Z7zep4",
"nbpages": {
"level": 1,
"link": "[1.3 Flow Control and Pseudocode](https://ndcbe.github.io/cbe-xx258/01.03-Flow-control.html#1.3-Flow-Control-and-Pseudocode)",
"section": "1.3 Flow Control and Pseudocode"
}
},
"source": [
"# 1.3 Flow Control and Pseudocode\n",
"\n",
"**Reference**: Chapter 2 of *Computational Nuclear Engineering and Radiological Science Using Python*, R. McClarren (2018)"
]
},
{
"cell_type": "markdown",
"metadata": {
"nbpages": {
"level": 2,
"link": "[1.3.1 Learning Objectives](https://ndcbe.github.io/cbe-xx258/01.03-Flow-control.html#1.3.1-Learning-Objectives)",
"section": "1.3.1 Learning Objectives"
}
},
"source": [
"## 1.3.1 Learning Objectives\n",
"\n",
"After studying this notebook, completing the activities, and asking questions in class, you should be able to:"
]
},
{
"cell_type": "markdown",
"metadata": {
"colab_type": "text",
"id": "LCciHuDFzerw",
"nbpages": {
"level": 2,
"link": "[1.3.2 If Statements](https://ndcbe.github.io/cbe-xx258/01.03-Flow-control.html#1.3.2-If-Statements)",
"section": "1.3.2 If Statements"
}
},
"source": [
"## 1.3.2 If Statements"
]
},
{
"cell_type": "markdown",
"metadata": {
"colab_type": "text",
"id": "4uw2c8PDzerw",
"nbpages": {
"level": 2,
"link": "[1.3.2 If Statements](https://ndcbe.github.io/cbe-xx258/01.03-Flow-control.html#1.3.2-If-Statements)",
"section": "1.3.2 If Statements"
}
},
"source": [
"Sometimes you want to execute code differently based on the value of a certain variable. This is most commonly done in if - else constructs. Here's an example"
]
},
{
"cell_type": "code",
"execution_count": 37,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 52
},
"colab_type": "code",
"executionInfo": {
"elapsed": 3241,
"status": "ok",
"timestamp": 1547736251653,
"user": {
"displayName": "Alexander Dowling",
"photoUrl": "https://lh3.googleusercontent.com/-LChdQ2m5OQE/AAAAAAAAAAI/AAAAAAAAAA0/JeXJe4vQJ7M/s64/photo.jpg",
"userId": "00988067626794866502"
},
"user_tz": 300
},
"id": "WFF0VKz5zerx",
"nbpages": {
"level": 2,
"link": "[1.3.2 If Statements](https://ndcbe.github.io/cbe-xx258/01.03-Flow-control.html#1.3.2-If-Statements)",
"section": "1.3.2 If Statements"
},
"outputId": "fc849cc8-bad5-4cdd-cbe0-f7c38b965d62"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"What is your opinion of student? \n",
"A\n"
]
}
],
"source": [
"instructors_opinion = input(\"What is your opinion of student? \") #'Not annoying'\n",
"grade = ''\n",
"if (instructors_opinion == 'annoying'):\n",
" grade = 'F+'\n",
"elif (instructors_opinion == 'Not annoying'):\n",
" grade = 'B+'\n",
"else:\n",
" grade = 'A'\n",
"print(grade)"
]
},
{
"cell_type": "markdown",
"metadata": {
"colab_type": "text",
"id": "oL2sMGZpzery",
"nbpages": {
"level": 2,
"link": "[1.3.2 If Statements](https://ndcbe.github.io/cbe-xx258/01.03-Flow-control.html#1.3.2-If-Statements)",
"section": "1.3.2 If Statements"
}
},
"source": [
"What this codes says is that if the value of instructors_opinion is \"annoying\", the grade will be \"F+\", else if (elif in python-speak) instructors_opinion is \"Not annoying\" the grade will be \"B+\", and anything else will be a grade of \"A\". In the example I typed in \"Not Annoying\" and the if statement and the elif statement require that the string exactly match, so it executed the else part of the code. (Note: This example came from the textbook. Prof. Dowling uses an objective grading scale in this class!)\n",
"\n"
]
},
{
"cell_type": "markdown",
"metadata": {
"colab_type": "text",
"id": "Gxtc4qFHzery",
"nbpages": {
"level": 2,
"link": "[1.3.2 If Statements](https://ndcbe.github.io/cbe-xx258/01.03-Flow-control.html#1.3.2-If-Statements)",
"section": "1.3.2 If Statements"
}
},
"source": [
"It is important to remember that when you want to check equality between two things you need to use == and **not a single equals sign**. A single equals sign is what you use when you want to assign something to a variable.\n",
"\n",
"
"
]
}
],
"metadata": {
"colab": {
"collapsed_sections": [],
"name": "L1-Getting-Started.ipynb",
"provenance": [],
"version": "0.3.2"
},
"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.3"
}
},
"nbformat": 4,
"nbformat_minor": 2
}