Skip to content
Snippets Groups Projects
Commit 0563d5b2 authored by parksjr5's avatar parksjr5
Browse files

adding Scope_C++

parent 0b1a6704
No related branches found
No related tags found
No related merge requests found
%% Cell type:code id:fc73d74c tags:
``` python
##ANSWER##
#Install answercheck in current director
from urllib.request import urlretrieve
urlretrieve('https://raw.githubusercontent.com/colbrydi/jupytercheck/master/answercheck.py', filename='answercheck.py')
##ANSWER##
```
%% Output
('answercheck.py', <http.client.HTTPMessage at 0x7fd334327910>)
%% Cell type:markdown id:3c2a4f39 tags:
# Scope
Recognizing where variables can be used in a program and where they cannot be used.
%% Cell type:markdown id:2b78245b tags:
**_Optional_** Motivating picture:
![title](scopePicture.jpg)(https://blogs.lse.ac.uk/impactofsocialsciences/files/2015/03/bridge-construction.jpg)
%% Cell type:markdown id:71e867d9 tags:
## Description
Scope is defined as when and where a variable is accessible in a program. There are different types of scope - different ways a variable can be accessed in a program. This document will only focus on two types of scope, Global scope and Local scope.
%% Cell type:markdown id:58ec20e8 tags:
## Self Assessment
Questions that test for the learning goals and allows students to evaluate if they truly understand the topics.
%% Cell type:markdown id:4af35ce7 tags:
&#9989; **<span style="color:red">Question 1:</span>** Which of the scope types is accessible to all of the program?
1. Local Scope
2. Global Scope
%% Cell type:code id:15f43828 tags:
``` python
##ANSWER##
2
##ANSWER##
```
%% Output
2
%% Cell type:markdown id:3921c156 tags:
&#9989; **<span style="color:red">Question 2:</span>** Where would you not see a local scope defined in a program?
1. In a function
2. In a class
3. In a loop
4. Beginning of a program
%% Cell type:code id:c3dfd97d tags:
``` python
##ANSWER##
4
##ANSWER##
```
%% Output
4
%% Cell type:markdown id:77bd1740 tags:
**Use the code below to answer questions 3-5.** <br/>
**Hint:** The print statement is written as the following:
``` bash
std::cout<< YourTextHere <<std::endl;
```
![title](GlobalScope.png)
%% Cell type:markdown id:12d2601a tags:
&#9989; **<span style="color:red">Question 3:</span>** What is printed at line 8?
%% Cell type:code id:65583bcc tags:
``` python
##ANSWER##
10
##ANSWER##
```
%% Output
10
%% Cell type:markdown id:93d6fe2f tags:
&#9989; **<span style="color:red">Question 4:</span>** What is printed at line 15?
%% Cell type:code id:e5e975d1 tags:
``` python
##ANSWER##
4
##ANSWER##
```
%% Output
4
%% Cell type:markdown id:87a7ef55 tags:
&#9989; **<span style="color:red">Question 5:</span>** If we were to print out **c** after the last yellow curly bracket, what would be its value?
%% Cell type:code id:53254c28 tags:
``` python
##ANSWER##
10
##ANSWER##
```
%% Output
10
%% Cell type:markdown id:5024c8b7 tags:
&#9989; **<span style="color:red">Question 6:</span>** What will be printed below?
%% Cell type:code id:1d37b9f3 tags:
``` python
x = 300
def func():
x = 200
print(x)
# print(x)
```
%% Cell type:code id:6c5c196b tags:
``` python
##ANSWER##
300
##ANSWER##
```
%% Output
300
%% Cell type:markdown id:0456a3c4 tags:
&#9989; **<span style="color:red">Question 7:</span>** What will be printed below?
%% Cell type:code id:f0e44800 tags:
``` python
x = 300
def func():
x = 200
print(x)
# func()
```
%% Cell type:code id:51029b03 tags:
``` python
##ANSWER##
200
##ANSWER##
```
%% Output
200
%% Cell type:markdown id:6b48b0d0 tags:
&#9989; **<span style="color:red">Question 8:</span>** How do you identify the scope of a variable in Python (As opposed to C++ syntax)?
1. Using curly brackets {}
2. Using parantheses ()
3. Using indentation
4. Using the variable name
%% Cell type:code id:4f70eba5 tags:
``` python
##ANSWER##
3
##ANSWER##
```
%% Output
3
%% Cell type:code id:9cc2b34a tags:
``` python
# from answercheck import checkanswer
# checkanswer.vector(x,'2cab95d1b144d663bad1ce5c51020ae0')
```
%% Cell type:markdown id:2a3fad10 tags:
## Training Materials
%% Cell type:markdown id:ea15c34b tags:
#### What is Scope?
%% Cell type:markdown id:343e3b11 tags:
&#9989; **<span style="color:red">Read the following website:</span>** https://www.educative.io/answers/what-is-scope <br>
**Note:** The website talks about where the scope is but not how to necessarily identify it in the code. When there are "curly brackets" or { }, the variables defined within this are considered **local** - in the case seen on the website, the following variable is local:
``` bash
string name = Bob
```
<br>
The video below will go more in depth about this, but this link will help first introduce you to Global and Local Scope.
%% Cell type:markdown id:b44650dd tags:
#### Introduction to Global and Local Scope in C++
%% Cell type:code id:b64de2b9 tags:
``` python
from IPython.display import YouTubeVideo
YouTubeVideo("RTWHa8WEtYk",width="100%", height=360, cc_load_policy=True)
```
%% Output
<IPython.lib.display.YouTubeVideo at 0x7fd334290520>
%% Cell type:markdown id:560d9310 tags:
#### Code Walkthrough of Scope in C++
%% Cell type:code id:3690ed8b tags:
``` python
from IPython.display import YouTubeVideo
YouTubeVideo("tXo7QyOGMBk",width="100%", height=360, cc_load_policy=True)
```
%% Output
<IPython.lib.display.YouTubeVideo at 0x7fd334290d60>
%% Cell type:markdown id:e8ae5f06 tags:
#### Scope in Python
%% Cell type:markdown id:7e9a0e12 tags:
&#9989; **<span style="color:red">Read the following website:</span>** https://www.w3schools.com/python/python_scope.asp <br>
%% Cell type:markdown id:44b461a0 tags:
---
Written by Jessica Parks, Michigan State University
As part of the Data Science Bridge Project
<a rel="license" href="http://creativecommons.org/licenses/by-nc/4.0/"><img alt="Creative Commons License" style="border-width:0" src="https://i.creativecommons.org/l/by-nc/4.0/88x31.png" /></a><br />This work is licensed under a <a rel="license" href="http://creativecommons.org/licenses/by-nc/4.0/">Creative Commons Attribution-NonCommercial 4.0 International License</a>.
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment