Created
May 7, 2020 21:20
-
-
Save tkphd/1c67306b6d69bbb791948cc90dcf5e35 to your computer and use it in GitHub Desktop.
Simple scripts to play with scope in R and Python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Ross is a programmer for Megabank, working on a new smartphone app that | |
# helps users transfer money between accounts. He's relatively new to | |
# Python, the language preferred by the top brass, and is not entirely | |
# sure how functions handle global variables. | |
# Ross opens a bank account, then uses the app to perform a series of | |
# transactions. Because he's no fool, he does so in the lobby of a | |
# Megabank branch, and the bank teller informs him after each step what | |
# his balance is. If everything is working correctly, Ross should be | |
# able to take back out exactly what he put in... unless the app is not | |
# working! | |
from sys import exit | |
total = 130 | |
print("Ross opens a new account with $", total) | |
def deposit(amount): | |
global total | |
if amount <= 0: | |
exit("Deposits must be positive!") | |
total += amount | |
print("${0} deposited. Your app balance is ${1}".format(amount, total)) | |
def withdraw(amount): | |
global total | |
if amount > total: | |
exit("You don't have that much money!") | |
total -= amount | |
print("${0} withdrawn. Your app balance is ${1}".format(amount, total)) | |
def balance(): | |
print("Your real balance is ${0}".format(total)) | |
w = 30 | |
print("Ross withdraws $", w) | |
withdraw(w) | |
balance() | |
d = 50 | |
print("Ross deposits $", d) | |
deposit(50) | |
balance() | |
w = 150 | |
print("Ross withdraws $", w) | |
withdraw(w) | |
balance() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Ross is a programmer for Megabank, working on a new smartphone app that | |
# helps users transfer money between accounts. He's relatively new to R, | |
# the language preferred by the top brass, and is not entirely sure of | |
# the difference between all the operators available for assigning | |
# values to variables. Specifically, there's this "scoped assignment" thing | |
# that sure looks like a harpoon to Ross. What's it for? | |
# Ross opens a bank account, then uses the app to perform a series of | |
# transactions. Because he's no fool, he does so in the lobby of a | |
# Megabank branch, and the bank teller informs him after each step what | |
# his balance is. If everything is working correctly, Ross should be able | |
# to take back out exactly what he put in... unless the app is not working! | |
scoped <- FALSE | |
open.account <- function(total) { | |
list( | |
deposit = function(amount) { | |
if(amount <= 0) | |
stop("Deposits must be positive!\n") | |
if (scoped) | |
total <<- total + amount | |
else | |
total <- total + amount | |
cat(amount, "deposited. Your balance is $", total, "in the app.\n\n") | |
}, | |
withdraw = function(amount) { | |
if(amount > total) | |
stop("You don't have that much money!\n") | |
if (scoped) | |
total <<- total - amount | |
else | |
total <- total - amount | |
cat(amount, "withdrawn. Your balance is $", total, "\n\n") | |
}, | |
balance = function() { | |
cat("Your balance is $", total, "in reality\n\n") | |
} | |
) | |
} | |
x <- 130 | |
cat("Ross opens a new account with $", x, "\n") | |
ross <- open.account(x) | |
w <- 30 | |
cat("Ross withdraws $", w, "\n") | |
ross$withdraw(w) | |
ross$balance() | |
d <- 50 | |
cat("Ross deposits $", d, "\n") | |
ross$deposit(50) | |
ross$balance() | |
w <- 150 | |
cat("Ross withdraws $", w, "\n") | |
ross$withdraw(w) | |
ross$balance() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment