diff --git a/.devcontainer/devcontainer.json b/.devcontainer/devcontainer.json new file mode 100644 index 000000000..db72d1c76 --- /dev/null +++ b/.devcontainer/devcontainer.json @@ -0,0 +1,57 @@ +{ + "name": "PreTeXt-Codespaces", + + // This Docker image includes some LaTeX support, but is still not to large. Note that if you keep your codespace running, it will use up your GitHub free storage quota. Additional options are listed below. + // "image": "oscarlevin/pretext:small", + // If you need to generate more complicated assets (such as sageplots) or use additional fonts when building to PDF, comment out the above line and uncomment the following line. + "image": "oscarlevin/pretext:full", + // If you only intend to build for web and don't have any latex-image generated assets, you can use a smaller image: + // "image": "oscarlevin/pretext:lite", + + + + // The following was the previous version of this file, which used the Codespaces base image. It is still available for reference, but is not recommended. + // "image": "mcr.microsoft.com/devcontainers/python:3", + // "features": { + // "ghcr.io/devcontainers/features/node:1": {}, + // "ghcr.io/rocker-org/devcontainer-features/pandoc:1": {} + // }, + // "forwardPorts": [ + // 27377, + // 27378 + // ], + // "portsAttributes": { + // "27378": { + // "label": "CodeChat", + // "onAutoForward": "openPreview", + // "requireLocalPort": true, + // "elevateIfNeeded": true, + // "protocol": "https" + // } + // }, + // "onCreateCommand": "pip install pretext", + // // Use 'postCreateCommand' to run commands after the container is created. + // "postCreateCommand": "sudo bash ./.devcontainer/postCreateCommand.sh", + + // Configure tool-specific properties. + "customizations": { + "codespaces": { + "openFiles": [ + "source/main.ptx" + ] + }, + "vscode": { + "settings": { + "editor.quickSuggestions": { + "other": "off" + }, + "editor.snippetSuggestions": "top", + "xml.validation.enabled": false + }, + "extensions": ["ms-vscode.live-server", "oscarlevin.pretext-tools"] + } + } + + // Uncomment to connect as root instead. More info: https://aka.ms/dev-containers-non-root. + // "remoteUser": "root" +} diff --git a/.gitignore b/.gitignore index 89bd4882e..ed41bfbfe 100644 --- a/.gitignore +++ b/.gitignore @@ -1,23 +1,91 @@ -__pycache__/ -build_info -build -published -.idea -.idea/* -pavement.pyc -# for builds on Mac: -**/.DS_Store -.vscode -*~ -*.bak -*.orig -\#*\# -sphinx-enki-info.txt -sphinx_settings.json -output.log -beta +# Boilerplate list of files in a PreTeXt project for git to ignore +# ensure this file is tracked +!.gitignore + +# don't track unpublished builds output -published -pdf -GenFigs -cli.log \ No newline at end of file + +# don't track assets generated from source +generated-assets + +# don't track node packages +node_modules + +# don't track error logs +.error_schema.log +cli.log + +# don't track OS related files (windows/macos/linux) +.DS_Store +.DS_Store? +._* +.AppleDouble +.LSOverride +.Spotlight-V100 +.Trashes +Icon +.AppleDB +.AppleDesktop +Network Trash Folder +Temporary Items +.apdisk +Thumbs.db +Thumbs.db:encryptable +ehthumbs.db +ehthumbs_vista.db +*.stackdump +*.lnk +*.cab +*.msi +*.msix +*.msm +*.msp +[Dd]esktop.ini +.directory +.fuse_hidden* +.Trash-* +.nfs* + +# Don't include VSCode generated files +.vscode +*.code-workspace + +# Don't inlucde SublimeText files +# Cache files for Sublime Text +*.tmlanguage.cache +*.tmPreferences.cache +*.stTheme.cache + +# Workspace files are user-specific +*.sublime-workspace + +# Project files should be checked into the repository, unless a significant +# proportion of contributors will probably not be using Sublime Text +*.sublime-project + +# SFTP configuration file +sftp-config.json +sftp-config-alt*.json + +# Package control specific files +Package Control.last-run +Package Control.ca-list +Package Control.ca-bundle +Package Control.system-ca-bundle +Package Control.cache/ +Package Control.ca-certs/ +Package Control.merged-ca-bundle +Package Control.user-ca-bundle +oscrypto-ca-bundle.crt +bh_unicode_properties.cache + +# Sublime-github package stores a github token in this file +# https://packagecontrol.io/packages/sublime-github +GitHub.sublime-settings + + +# Don't include Dropbox settings and caches +.dropbox +.dropbox.attr +.dropbox.cache + diff --git a/pretext/ClassesBasics/UserDefinedClasses.ptx b/pretext/ClassesBasics/UserDefinedClasses.ptx index 7a7a52a72..5bdd8ee1a 100644 --- a/pretext/ClassesBasics/UserDefinedClasses.ptx +++ b/pretext/ClassesBasics/UserDefinedClasses.ptx @@ -166,33 +166,33 @@ print(x, y)

True True

- +

Look closely at how the objects are instantiated. - +

True False

- +

Look closely at how the objects are instantiated and the types of the objects. - +

False True

- +

Correct, the BMW object is not the Tesla object but they are of the same type. - +

False False

- +

Look closer at types of the objects. - +

diff --git a/pretext/Debugging/HowtoAvoidDebugging.ptx b/pretext/Debugging/HowtoAvoidDebugging.ptx index 3b643b2a7..3695d07e3 100644 --- a/pretext/Debugging/HowtoAvoidDebugging.ptx +++ b/pretext/Debugging/HowtoAvoidDebugging.ptx @@ -75,25 +75,25 @@ print(final_time)

Python is stupid and does not know how to add properly.

- +

No, Python is probabaly not broken. - +

There is nothing wrong here.

- +

No, try adding the two numbers together yourself, you will definitely get a different result. - +

Python is doing string concatenation, not integer addition.

- +

Yes! Remember that input returns a string. Now we will need to convert the string to an integer - +

diff --git a/pretext/Debugging/KnowyourerrorMessages.ptx b/pretext/Debugging/KnowyourerrorMessages.ptx index 2a6e62db1..3b2021c0b 100644 --- a/pretext/Debugging/KnowyourerrorMessages.ptx +++ b/pretext/Debugging/KnowyourerrorMessages.ptx @@ -44,25 +44,25 @@ print(final_time_int)

You cannot use a variable on both the left and right hand sides of an assignment statement.

- +

No, You can, as long as all the variables on the right hand side already have values. - +

wait_time_int does not have a value so it cannot be used on the right hand side.

- +

Yes. Variables must already have values in order to be used on the right hand side. - +

This is not really an error, Python is broken.

- +

No, No, No! - +

diff --git a/pretext/Dictionaries/Aliasingandcopying.ptx b/pretext/Dictionaries/Aliasingandcopying.ptx index cb6ed85fc..968663cb6 100644 --- a/pretext/Dictionaries/Aliasingandcopying.ptx +++ b/pretext/Dictionaries/Aliasingandcopying.ptx @@ -45,33 +45,33 @@ print(mydict["elephant"])

23

- +

mydict and yourdict are both names for the same dictionary. - +

None

- +

The dictionary is mutable so changes can be made to the keys and values. - +

999

- +

Yes, since yourdict is an alias for mydict, the value for the key elephant has been changed. - +

Error, there are two different keys named elephant.

- +

There is only one dictionary with only one key named elephant. The dictionary has two different names, mydict and yourdict. - +

diff --git a/pretext/Dictionaries/Dictionarymethods.ptx b/pretext/Dictionaries/Dictionarymethods.ptx index de2023b52..ab5884ddb 100644 --- a/pretext/Dictionaries/Dictionarymethods.ptx +++ b/pretext/Dictionaries/Dictionarymethods.ptx @@ -179,33 +179,33 @@ print(keylist[3])

cat

- +

keylist is a list of all the keys which is then sorted. cat would be at index 1. - +

dog

- +

keylist is a list of all the keys which is then sorted. dog would be at index 2. - +

elephant

- +

Yes, the list of keys is sorted and the item at index 3 is printed. - +

bear

- +

keylist is a list of all the keys which is then sorted. bear would be at index 0. - +

@@ -225,33 +225,33 @@ print(answer)

2

- +

get returns the value associated with a given key so this divides 12 by 6. - +

0.5

- +

12 is divided by 6, not the other way around. - +

bear

- +

Take another look at the example for get above. get returns the value associated with a given key. - +

Error, divide is not a valid operation on dictionaries.

- +

The integer division operator is being used on the values returned from the get method, not on the dictionary. - +

@@ -270,17 +270,17 @@ print("dog" in mydict)

True

- +

Yes, dog is a key in the dictionary. - +

False

- +

The in operator returns True if a key is in the dictionary, False otherwise. - +

@@ -299,17 +299,17 @@ print(23 in mydict)

True

- +

23 is a value in the dictionary, not a key. - +

False

- +

Yes, the in operator returns True if a key is in the dictionary, False otherwise. - +

@@ -332,33 +332,33 @@ print(total)

18

- +

Add the values that have keys greater than 3, not equal to 3. - +

43

- +

Yes, the for statement iterates over the keys. It adds the values of the keys that have length greater than 3. - +

0

- +

This is the accumulator pattern. total starts at 0 but then changes as the iteration proceeds. - +

61

- +

Not all the values are added together. The if statement only chooses some of them. - +

diff --git a/pretext/Dictionaries/Dictionaryoperations.ptx b/pretext/Dictionaries/Dictionaryoperations.ptx index fd1e32bc5..245c73d95 100644 --- a/pretext/Dictionaries/Dictionaryoperations.ptx +++ b/pretext/Dictionaries/Dictionaryoperations.ptx @@ -55,33 +55,33 @@ print(mydict["mouse"])

12

- +

12 is associated with the key cat. - +

0

- +

The key mouse will be associated with the sum of the two values. - +

18

- +

Yes, add the value for cat and the value for dog (12 + 6) and create a new entry for mouse. - +

Error, there is no entry with mouse as the key.

- +

Since the new key is introduced on the left hand side of the assignment statement, a new key-value pair is added to the dictionary. - +

diff --git a/pretext/Dictionaries/intro-Dictionaries.ptx b/pretext/Dictionaries/intro-Dictionaries.ptx index 5caac95fc..4bfd86c3b 100644 --- a/pretext/Dictionaries/intro-Dictionaries.ptx +++ b/pretext/Dictionaries/intro-Dictionaries.ptx @@ -77,17 +77,17 @@ print(value)

False

- +

Dictionaries associate keys with values but there is no assumed order for the entries. - +

True

- +

Yes, dictionaries are associative collections meaning that they store key-value pairs. - +

@@ -106,33 +106,33 @@ print(mydict["dog"])

12

- +

12 is associated with the key cat. - +

6

- +

Yes, 6 is associated with the key dog. - +

23

- +

23 is associated with the key elephant. - +

Error, you cannot use the index operator with a dictionary.

- +

The [ ] operator, when used with a dictionary, will look up a value based on its key. - +

diff --git a/pretext/Functions/FlowofExecutionSummary.ptx b/pretext/Functions/FlowofExecutionSummary.ptx index 2c27227b0..57514b857 100644 --- a/pretext/Functions/FlowofExecutionSummary.ptx +++ b/pretext/Functions/FlowofExecutionSummary.ptx @@ -53,41 +53,41 @@ print(result)

1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11

- +

Although Python typically processes lines in order from top to bottom, function definitions and calls are an exception to this rule. - +

1, 2, 3, 5, 6, 7, 9, 10, 11

- +

Although Python typically processes lines in order from top to bottom, function definitions and calls are an exception to this rule. Although this order skips blank lines, it still lists the lines of code in order. - +

9, 10, 11, 1, 2, 3, 5, 6, 7

- +

This is close, in that Python will not execute the functions until after they are called, but there are two problems here. First, Python does not know which lines are function definitions until it processes them, so it must at least process the function headers before skipping over the functions. Section, notice that line 10 involves a function call. Python must execute the function square before moving on to line 11. - +

9, 10, 5, 6, 1, 2, 3, 6, 7, 10, 11

- +

This is close, in that Python will not execute the functions until after they are called, but there is one problem here. Python does not know which lines are function definitions until it processes them, so it must at least process the function headers before skipping over the functions. - +

1, 5, 9, 10, 5, 6, 1, 2, 3, 6, 7, 10, 11

- +

Python starts at line 1, notices that it is a function definition and skips over all of the lines in the function definition until it finds a line that it no longer included in the function (line 5). It then notices line 5 is also a function definition and again skips over the function body to line 9. On line 10 it notices it has a function to execute, so it goes back and executes that function. Notice that that function includes another function call. It returns from the function call and completes the assignment in line 6. Then it returns the result of line 7 and completes the assignment in line 10. Finally, it will go to line 11 after the function square and the assignment are complete. - +

@@ -116,33 +116,33 @@ print(result)

25

- +

The function square returns the square of its input (via a call to pow) - +

5

- +

What is printed is the output of the square function. 5 is the input to the square function. - +

125

- +

Notice that pow is called from within square with a base (b) of 5 and a power (p) of two. - +

32

- +

Notice that pow is called from within square with a base (b) of 5 and a power (p) of two. - +

diff --git a/pretext/Functions/Functionsthatreturnvalues.ptx b/pretext/Functions/Functionsthatreturnvalues.ptx index 9cda6247f..4423bbea2 100644 --- a/pretext/Functions/Functionsthatreturnvalues.ptx +++ b/pretext/Functions/Functionsthatreturnvalues.ptx @@ -170,33 +170,33 @@ def addEm(x, y, z):

You should never use a print statement in a function definition.

- +

Although you should not mistake print for return, you may include print statements inside your functions. - +

You should not have any statements in a function after the return statement. Once the function gets to the return statement it will immediately stop executing the function.

- +

This is a very common mistake so be sure to watch out for it when you write your code! - +

You must calculate the value of x+y+z before you return it.

- +

Python will automatically calculate the value x+y+z and then return it in the statement as it is written - +

A function cannot return a number.

- +

Functions can return any legal data, including (but not limited to) numbers, strings, turtles, etc. - +

@@ -215,25 +215,25 @@ def addEm(x, y, z):

None

- +

We have accidentally used print where we mean return. Therefore, the function will return the value None by default. This is a VERY COMMON mistake so watch out! This mistake is also particularly difficult to find because when you run the function the output looks the same. It is not until you try to assign its value to a variable that you can notice a difference. - +

The value of x + y + z

- +

Careful! This is a very common mistake. Here we have printed the value x+y+z but we have not returned it. To return a value we MUST use the return keyword. - +

The string 'x + y + z'

- +

x+y+z calculates a number (assuming x+y+z are numbers) which represents the sum of the values x, y and z. - +

diff --git a/pretext/Functions/TheAccumulatorPattern.ptx b/pretext/Functions/TheAccumulatorPattern.ptx index f875e2b51..60ca60e95 100644 --- a/pretext/Functions/TheAccumulatorPattern.ptx +++ b/pretext/Functions/TheAccumulatorPattern.ptx @@ -106,33 +106,33 @@ def square(x):

The square function will return x instead of x * x

- +

The variable runningtotal will be reset to 0 each time through the loop. However because this assignment happens as the first instruction, the next instruction in the loop will set it back to x. When the loop finishes, it will have the value x, which is what is returned. - +

The square function will cause an error

- +

Assignment statements are perfectly legal inside loops and will not cause an error. - +

The square function will work as expected and return x * x

- +

By putting the statement that sets runningtotal to 0 inside the loop, that statement gets executed every time through the loop, instead of once before the loop begins. The result is that runningtotal is 'cleared' (reset to 0) each time through the loop. - +

The square function will return 0 instead of x * x

- +

The line runningtotal = 0 is the first line in the for loop, but immediately after this line, the line runningtotal = runningtotal + x will execute, giving runningtotal a non-zero value (assuming x is non-zero). - +

diff --git a/pretext/Functions/UnitTesting.ptx b/pretext/Functions/UnitTesting.ptx index e39b6a024..a345272a9 100644 --- a/pretext/Functions/UnitTesting.ptx +++ b/pretext/Functions/UnitTesting.ptx @@ -62,41 +62,41 @@ assert type(9.0//5) == int

A runtime error will occur

- +

The expression ``x==y`` evaluates to ``True`` - +

A message is printed out saying that the test failed.

- +

The expression ``x==y`` evaluates to ``True`` - +

x will get the value that y currently has

- +

``x==y`` is a Boolean expression, not an assignment statement - +

Nothing will happen

- +

The expression ``x==y`` evaluates to ``True`` - +

A message is printed out saying that the test passed.

- +

When an assertion test passes, no message is printed. - +

@@ -159,25 +159,25 @@ assert square(3) == 9

assert blanked('under', 'du', 'u_d__') == True

- +

blanked only takes two inputs; this provides three inputs to the blanked function - +

assert blanked('under', 'u_d__') == 'du'

- +

The second argument to the blanked function should be the letters that have been guessed, not the blanked version of the word - +

assert blanked('under', 'du') == 'u_d__'

- +

This checks whether the value returned from the blanked function is 'u_d__'. - +

diff --git a/pretext/Functions/Variablesandparametersarelocal.ptx b/pretext/Functions/Variablesandparametersarelocal.ptx index 3451b4f86..b3231a3db 100644 --- a/pretext/Functions/Variablesandparametersarelocal.ptx +++ b/pretext/Functions/Variablesandparametersarelocal.ptx @@ -120,25 +120,25 @@ print(result)

Its value

- +

Value is the contents of the variable. Scope concerns where the variable is "known". - +

The range of statements in the code where a variable can be accessed.

- +

The range in which a variable can be accessed is it's scope. For example if a variable is defined in a function, then the scope of the variable is that function. - +

Its name

- +

The name of a variable is just an identifier or alias. Scope concerns where the variable is "known". - +

@@ -151,25 +151,25 @@ print(result)

A temporary variable that is only used inside a function

- +

Yes, a local variable is a temporary variable that is only known (only exists) in the function it is defined in. - +

The same as a parameter

- +

While parameters may be considered local variables, functions may also define and use additional local variables. - +

Another name for any variable

- +

Variables that are used outside a function are not local, but rather global variables. - +

@@ -182,25 +182,25 @@ print(result)

Yes, and there is no reason not to.

- +

While there is no problem as far as Python is concerned, it is generally considered bad style because of the potential for the programmer to get confused. - +

Yes, but it is considered bad form.

- +

it is generally considered bad style because of the potential for the programmer to get confused. If you must use global variables (also generally bad form) make sure they have unique names. - +

No, it will cause an error.

- +

Python manages global and local scope separately and has clear rules for how to handle variables with the same name in different scopes, so this will not cause a Python error. - +

diff --git a/pretext/Functions/functions.ptx b/pretext/Functions/functions.ptx index 4c4af30eb..1cd165d7c 100644 --- a/pretext/Functions/functions.ptx +++ b/pretext/Functions/functions.ptx @@ -189,33 +189,33 @@ wn.exitonclick()

A named sequence of statements.

- +

Yes, a function is a named sequence of statements. - +

Any sequence of statements.

- +

While functions contain sequences of statements, not all sequences of statements are considered functions. - +

A mathematical expression that calculates a value.

- +

While some functions do calculate values, the python idea of a function is slightly different from the mathematical idea of a function in that not all functions calculate values. Consider, for example, the turtle functions in this section. They made the turtle draw a specific shape, rather than calculating a value. - +

A statement of the form x = 5 + 4.

- +

This statement is called an assignment statement. It assigns the value on the right (9), to the name on the left (x). - +

@@ -228,33 +228,33 @@ wn.exitonclick()

To improve the speed of execution

- +

Functions have little effect on how fast the program runs. - +

To help the programmer organize programs into chunks that match how they think about the solution to the problem.

- +

While functions are not required, they help the programmer better think about the solution by organizing pieces of the solution into logical chunks that can be reused. - +

All Python programs must be written using functions

- +

In the first several chapters, you have seen many examples of Python programs written without the use of functions. While writing and using functions is desirable and essential for good programming style as your programs get longer, it is not required. - +

To calculate values.

- +

Not all functions calculate values. - +

@@ -267,33 +267,33 @@ wn.exitonclick()

def drawCircle(t):

- +

A function may take zero or more parameters. It does not have to have two. In this case the size of the circle might be specified in the body of the function. - +

def drawCircle:

- +

A function needs to specify its parameters in its header. - +

drawCircle(t, sz):

- +

A function definition needs to include the keyword def. - +

def drawCircle(t, sz)

- +

A function definition header must end in a colon (:). - +

@@ -315,33 +315,33 @@ def drawSquare(t, sz):

def drawSquare(t, sz)

- +

This line is the complete function header (except for the semi-colon) which includes the name as well as several other components. - +

drawSquare

- +

Yes, the name of the function is given after the keyword def and before the list of parameters. - +

drawSquare(t, sz)

- +

This includes the function name and its parameters - +

Make turtle t draw a square with side sz.

- +

This is a comment stating what the function does. - +

@@ -363,33 +363,33 @@ def drawSquare(t, sz):

i

- +

i is a variable used inside of the function, but not a parameter, which is passed in to the function. - +

t

- +

t is only one of the parameters to this function. - +

t, sz

- +

Yes, the function specifies two parameters: t and sz. - +

t, sz, i

- +

the parameters include only those variables whose values that the function expects to receive as input. They are specified in the header of the function. - +

@@ -411,41 +411,41 @@ def drawSquare(t, sz):

def drawSquare(t, sz)

- +

No, t and sz are the names of the formal parameters to this function. When the function is called, it requires actual values to be passed in. - +

drawSquare

- +

A function call always requires parentheses after the name of the function. - +

drawSquare(10)

- +

This function takes two parameters (arguments) - +

drawSquare(alex, 10):

- +

A colon is only required in a function definition. It will cause an error with a function call. - +

drawSquare(alex, 10)

- +

Since alex was already previously defined and 10 is a value, we have passed in two correct values for this function. - +

@@ -458,17 +458,17 @@ def drawSquare(t, sz):

True

- +

Yes, you can call a function multiple times by putting the call in a loop. - +

False

- +

One of the purposes of a function is to allow you to call it more than once. Placing it in a loop allows it to executed multiple times as the body of the loop runs multiple times. - +

diff --git a/pretext/GeneralIntro/ATypicalFirstProgram.ptx b/pretext/GeneralIntro/ATypicalFirstProgram.ptx index 4bb8512e0..c45f29df9 100644 --- a/pretext/GeneralIntro/ATypicalFirstProgram.ptx +++ b/pretext/GeneralIntro/ATypicalFirstProgram.ptx @@ -35,33 +35,33 @@ print("Hello, World!")

sends information to the printer to be printed on paper.

- +

Within the Python programming language, the print function has nothing to do with the printer. - +

displays a value on the screen.

- +

Yes, the print function is used to display the value of the thing being printed. - +

tells the computer to put the information in print, rather than cursive, format.

- +

The format of the information is called its font and has nothing to do with the print function. - +

tells the computer to speak the information.

- +

That would be a different function. - +

diff --git a/pretext/GeneralIntro/Algorithms.ptx b/pretext/GeneralIntro/Algorithms.ptx index f11cdf1ad..716b5fbf3 100644 --- a/pretext/GeneralIntro/Algorithms.ptx +++ b/pretext/GeneralIntro/Algorithms.ptx @@ -19,33 +19,33 @@

To think like a computer.

- +

Computers do not think, they only do what we humans tell them to do via programs. - +

To be able to write code really well.

- +

While it is necessary for most computer scientists to know how to write code, it is not the most important skill. - +

To be able to solve problems.

- +

Computer scientists are all about solving problems. We use computers to automate solutions to problems and to do things faster and more accurately than we can do by hand or manually. - +

To be really good at math.

- +

Computer science and math are similar in many ways and it helps to have a strong mathematical foundation, but you do not have to be good at math to be a good computer scientist. - +

@@ -58,33 +58,33 @@

A solution to a problem that can be solved by a computer.

- +

While it is true that algorithms often do solve problems, this is not the best answer. An algorithm is more than just the solution to the problem for a computer. An algorithm can be used to solve all sorts of problems, including those that have nothing to do with computers. - +

A step by step list of instructions that if followed exactly will solve the problem under consideration.

- +

Algorithms are like recipes: they must be followed exactly, they must be clear and unambiguous, and they must end. - +

A series of instructions implemented in a programming language.

- +

Programming languages are used to express algorithms, but an algorithm does not have to be expressed in terms of a programming language. - +

A special kind of notation used by computer scientists.

- +

Computer scientists sometimes use a special notation to illustrate or document an algorithm, but this is not the definition of an algorithm. - +

diff --git a/pretext/GeneralIntro/Comments.ptx b/pretext/GeneralIntro/Comments.ptx index 8c3159fd1..ffde07d21 100644 --- a/pretext/GeneralIntro/Comments.ptx +++ b/pretext/GeneralIntro/Comments.ptx @@ -37,33 +37,33 @@ print("Hello, World!") # Isn't this easy!

To tell the computer what you mean in your program.

- +

Comments are ignored by the computer. - +

For the people who are reading your code to know, in natural language, what the program is doing.

- +

The computer ignores comments. It's for the humans that will "consume" your program. - +

Nothing, they are extraneous information that is not needed.

- +

Comments can provide much needed information for anyone reading the program. - +

Nothing in a short program. They are only needed for really large programs.

- +

Even small programs benefit from comments. - +

diff --git a/pretext/GeneralIntro/ExperimentalDebugging.ptx b/pretext/GeneralIntro/ExperimentalDebugging.ptx index 7ad0526b7..9f1f66384 100644 --- a/pretext/GeneralIntro/ExperimentalDebugging.ptx +++ b/pretext/GeneralIntro/ExperimentalDebugging.ptx @@ -37,33 +37,33 @@

programming is the process of writing and gradually debugging a program until it does what you want.

- +

Programming is the writing of the source code and debugging is the process of finding and correcting all the errors within the program until it is correct. - +

programming is creative and debugging is routine.

- +

Programming can be creative, but it also follows a process and debugging can involve creativity in how you find the errors. - +

programming is fun and debugging is work.

- +

Some people think that debugging is actually more fun than programming (they usually become good software testers). Debugging is much like solving puzzles, which some people think is fun! - +

there is no difference between them.

- +

You cannot debug without first having a program, meaning that someone had to do the programming first. - +

diff --git a/pretext/GeneralIntro/FormalandNaturalLanguages.ptx b/pretext/GeneralIntro/FormalandNaturalLanguages.ptx index 66e4e3d88..cc665d4bb 100644 --- a/pretext/GeneralIntro/FormalandNaturalLanguages.ptx +++ b/pretext/GeneralIntro/FormalandNaturalLanguages.ptx @@ -109,33 +109,33 @@

natural languages can be parsed while formal languages cannot.

- +

Actually both languages can be parsed (determining the structure of the sentence), but formal languages can be parsed more easily in software. - +

ambiguity, redundancy, and literalness.

- +

All of these can be present in natural languages, but cannot exist in formal languages. - +

there are no differences between natural and formal languages.

- +

There are several differences between the two but they are also similar. - +

tokens, structure, syntax, and semantics.

- +

These are the similarities between the two. - +

@@ -148,17 +148,17 @@

True

- +

It usually takes longer to read a program because the structure is as important as the content and must be interpreted in smaller pieces for understanding. - +

False

- +

It usually takes longer to read a program because the structure is as important as the content and must be interpreted in smaller pieces for understanding. - +

diff --git a/pretext/GeneralIntro/MoreAboutPrograms.ptx b/pretext/GeneralIntro/MoreAboutPrograms.ptx index bfcecddda..9751640a4 100644 --- a/pretext/GeneralIntro/MoreAboutPrograms.ptx +++ b/pretext/GeneralIntro/MoreAboutPrograms.ptx @@ -52,33 +52,33 @@

a sequence of instructions that specifies how to perform a computation.

- +

It is just step-by-step instructions that the computer can understand and execute. Programs often implement algorithms, but note that algorithms are typically less precise than programs and do not have to be written in a programming language. - +

something you follow along at a play or concert.

- +

True, but not in this context. We mean a program as related to a computer. - +

a computation, even a symbolic computation.

- +

A program can perform a computation, but by itself it is not one. - +

the same thing as an algorithm.

- +

Programs often implement algorithms, but they are not the same thing. An algorithm is a step by step list of instructions, but those instructions are not necessarily precise enough for a computer to follow. A program must be written in a programming language that the computer knows how to interpret. - +

diff --git a/pretext/GeneralIntro/RuntimeErrors.ptx b/pretext/GeneralIntro/RuntimeErrors.ptx index d489fc253..9d946072e 100644 --- a/pretext/GeneralIntro/RuntimeErrors.ptx +++ b/pretext/GeneralIntro/RuntimeErrors.ptx @@ -19,25 +19,25 @@

Attempting to divide by 0.

- +

Python cannot reliably tell if you are trying to divide by 0 until it is executing your program (e.g., you might be asking the user for a value and then dividing by that value - you cannot know what value the user will enter before you run the program). - +

Forgetting a colon at the end of a statement where one is required.

- +

This is a problem with the formal structure of the program. Python knows where colons are required and can detect when one is missing simply by looking at the code without running it. - +

Forgetting to divide by 100 when printing a percentage amount.

- +

This will produce the wrong answer, but Python will not consider it an error at all. The programmer is the one who understands that the answer produced is wrong. - +

@@ -50,33 +50,33 @@

The programmer.

- +

Programmers rarely find all the runtime errors, there is a computer program that will do it for us. - +

The interpreter.

- +

If an instruction is illegal to perform at that point in the execution, the interpreter will stop with a message describing the exception. - +

The computer.

- +

Well, sort of. But it is a special thing in the computer that does it. The stand alone computer without this additional piece can not do it. - +

The teacher / instructor.

- +

Your teacher and instructor may be able to find most of your runtime errors, but only because they have experience looking at code and possibly writing code. With experience runtime errors are easier to find. But we also have an automated way of finding these types of errors. - +

diff --git a/pretext/GeneralIntro/SemanticErrors.ptx b/pretext/GeneralIntro/SemanticErrors.ptx index 748edfe1c..8c61407c0 100644 --- a/pretext/GeneralIntro/SemanticErrors.ptx +++ b/pretext/GeneralIntro/SemanticErrors.ptx @@ -21,25 +21,25 @@

Attempting to divide by 0.

- +

A semantic error is an error in logic. In this case the program does not produce the correct output because the problem is not solved correctly. This would be considered a run-time error. - +

Forgetting a colon at the end of a statement where one is required.

- +

A semantic error is an error in logic. In this case the program does not produce the correct output because the code can not be processed by the compiler or interpreter. This would be considered a syntax error. - +

Forgetting to divide by 100 when printing a percentage amount.

- +

This will produce the wrong answer because the programmer implemented the solution incorrectly. This is a semantic error. - +

@@ -52,33 +52,33 @@

The programmer.

- +

You must fully understand the problem so the you can tell if your program properly solves it. - +

The compiler / interpreter.

- +

The compiler and / or interpreter will only do what you instruct it to do. It does not understand what the problem is that you want to solve. - +

The computer.

- +

The computer does not understand your problem. It just executes the instructions that it is given. - +

The teacher / instructor.

- +

Your teacher and instructor may be able to find most of your semantic errors, but only because they have experience solving problems. However it is your responsibility to understand the problem so you can develop a correct solution. - +

diff --git a/pretext/GeneralIntro/SpecialWaystoExecutePythoninthisBook.ptx b/pretext/GeneralIntro/SpecialWaystoExecutePythoninthisBook.ptx index 501724dda..1fee436af 100644 --- a/pretext/GeneralIntro/SpecialWaystoExecutePythoninthisBook.ptx +++ b/pretext/GeneralIntro/SpecialWaystoExecutePythoninthisBook.ptx @@ -74,33 +74,33 @@ print(2 + 3)

save programs and reload saved programs.

- +

You can (and should) save the contents of the activecode window. - +

type in Python source code.

- +

You are not limited to running the examples that are already there. Try adding to them and creating your own. - +

execute Python code right in the text itself within the web browser.

- +

The activecode interpreter will allow you type Python code into the textbox and then you can see it execute as the interpreter interprets and executes the source code. - +

receive a yes/no answer about whether your code is correct or not.

- +

Although you can (and should) verify that your code is correct by examining its output, activecode will not directly tell you whether you have correctly implemented your program. - +

@@ -113,33 +113,33 @@ print(2 + 3)

measure the speed of a program's execution.

- +

In fact, codelens steps through each line one by one as you click, which is MUCH slower than the Python interpreter. - +

control the step by step execution of a program.

- +

By using codelens, you can control the execution of a program step by step. You can even go backwards! - +

write and execute your own Python code.

- +

Codelens works only for the pre-programmed examples. - +

execute the Python code that is in codelens.

- +

By stepping forward through the Python code in codelens, you are executing the Python program. - +

diff --git a/pretext/GeneralIntro/Syntaxerrors.ptx b/pretext/GeneralIntro/Syntaxerrors.ptx index 033e6ad93..3a7d1b2a9 100644 --- a/pretext/GeneralIntro/Syntaxerrors.ptx +++ b/pretext/GeneralIntro/Syntaxerrors.ptx @@ -25,25 +25,25 @@

Attempting to divide by 0.

- +

A syntax error is an error in the structure of the python code that can be detected before the program is executed. Python cannot usually tell if you are trying to divide by 0 until it is executing your program (e.g., you might be asking the user for a value and then dividing by that value - you cannot know what value the user will enter before you run the program). - +

Forgetting a colon at the end of a statement where one is required.

- +

This is a problem with the formal structure of the program. Python knows where colons are required and can detect when one is missing simply by looking at the code without running it. - +

Forgetting to divide by 100 when printing a percentage amount.

- +

This will produce the wrong answer, but Python will not consider it an error at all. The programmer is the one who understands that the answer produced is wrong. - +

@@ -56,33 +56,33 @@

The programmer.

- +

Programmers rarely find all the syntax errors, there is a computer program that will do it for us. - +

The compiler / interpreter.

- +

The compiler and / or interpreter is a computer program that determines if your program is written in a way that can be translated into machine language for execution. - +

The computer.

- +

Well, sort of. But it is a special thing in the computer that does it. The stand alone computer without this additional piece can not do it. - +

The teacher / instructor.

- +

Your teacher and instructor may be able to find most of your syntax errors, but only because they have experience looking at code and possibly writing code. With experience syntax errors are easier to find. But we also have an automated way of finding these types of errors. - +

diff --git a/pretext/GeneralIntro/ThePythonProgrammingLanguage.ptx b/pretext/GeneralIntro/ThePythonProgrammingLanguage.ptx index 237728fcb..52001c622 100644 --- a/pretext/GeneralIntro/ThePythonProgrammingLanguage.ptx +++ b/pretext/GeneralIntro/ThePythonProgrammingLanguage.ptx @@ -97,33 +97,33 @@ My first program adds two numbers, 2 and 3:

the instructions in a program, stored in a file.

- +

The file that contains the instructions written in the high level language is called the source code file. - +

the language that you are programming in (e.g., Python).

- +

This language is simply called the programming language, or simply the language. - +

the environment/tool in which you are programming.

- +

The environment may be called the IDE, or integrated development environment, though not always. - +

the number (or "code") that you must input at the top of each program to tell the computer how to execute your program.

- +

There is no such number that you must type in at the start of your program. - +

@@ -138,33 +138,33 @@ My first program adds two numbers, 2 and 3:

It is high-level if you are standing and low-level if you are sitting.

- +

In this case high and low have nothing to do with altitude. - +

It is high-level if you are programming for a computer and low-level if you are programming for a phone or mobile device.

- +

High and low have nothing to do with the type of device you are programming for. Instead, look at what it takes to run the program written in the language. - +

It is high-level if the program must be processed before it can run, and low-level if the computer can execute it without additional processing.

- +

Python is a high level language but must be interpreted into machine code (binary) before it can be executed. - +

It is high-level if it easy to program in and is very short; it is low-level if it is really hard to program in and the programs are really long.

- +

While it is true that it is generally easier to program in a high-level language and programs written in a high-level language are usually shorter, this is not always the case. - +

@@ -177,33 +177,33 @@ My first program adds two numbers, 2 and 3:

1 = a process, 2 = a function

- +

Compiling is a software process, and running the interpreter is invoking a function, but how is a process different than a function? - +

1 = translating an entire book, 2 = translating a line at a time

- +

Compilers take the entire source code and produce object code or the executable and interpreters execute the code line by line. - +

1 = software, 2 = hardware

- +

Both compilers and interpreters are software. - +

1 = object code, 2 = byte code

- +

Compilers can produce object code or byte code depending on the language. An interpreter produces neither. - +

diff --git a/pretext/GeneralIntro/WhatisDebugging.ptx b/pretext/GeneralIntro/WhatisDebugging.ptx index 5be266672..d721c4bd3 100644 --- a/pretext/GeneralIntro/WhatisDebugging.ptx +++ b/pretext/GeneralIntro/WhatisDebugging.ptx @@ -21,33 +21,33 @@

tracking down programming errors and correcting them.

- +

Programming errors are called bugs and the process of finding and removing them from a program is called debugging. - +

removing all the bugs from your house.

- +

Maybe, but that is not what we are talking about in this context. - +

finding all the bugs in the program.

- +

This is partially correct. But, debugging is more than just finding the bugs. What do you need to do once you find them? - +

fixing the bugs in the program.

- +

This is partially correct. But, debugging is more than just fixing the bugs. What do you need to do before you can fix them? - +

diff --git a/pretext/IntroRecursion/TheThreeLawsofRecursion.ptx b/pretext/IntroRecursion/TheThreeLawsofRecursion.ptx index 9d4bddde1..d0b975052 100644 --- a/pretext/IntroRecursion/TheThreeLawsofRecursion.ptx +++ b/pretext/IntroRecursion/TheThreeLawsofRecursion.ptx @@ -55,33 +55,33 @@

6

- +

There are only five numbers on the list, the number of recursive calls will not be greater than the size of the list. - +

5

- +

The initial call to listsum is not a recursive call. - +

4

- +

the first recursive call passes the list [4,6,8,10], the second [6,8,10] and so on until [10]. - +

3

- +

This would not be enough calls to cover all the numbers on the list - +

@@ -94,33 +94,33 @@

n == 0

- +

Although this would work there are better and slightly more efficient choices. since fact(1) and fact(0) are the same. - +

n == 1

- +

A good choice, but what happens if you call fact(0)? - +

n >= 0

- +

This basecase would be true for all numbers greater than zero so fact of any positive number would be 1. - +

n <= 1

- +

Good, this is the most efficient, and even keeps your program from crashing if you try to compute the factorial of a negative number. - +

diff --git a/pretext/Lists/AccessingElements.ptx b/pretext/Lists/AccessingElements.ptx index d9cae3bd3..f282604fb 100644 --- a/pretext/Lists/AccessingElements.ptx +++ b/pretext/Lists/AccessingElements.ptx @@ -34,25 +34,25 @@ print(alist[5])

[ ]

- +

The empty list is at index 4. - +

3.14

- +

Yes, 3.14 is at index 5 since we start counting at 0 and sublists count as one item. - +

False

- +

False is at index 6. - +

@@ -71,25 +71,25 @@ print(alist[2].upper())

Error, you cannot use the upper method on a list.

- +

alist[2] is the string cat so the upper method is legal - +

2

- +

2 is the index. We want the item at that index. - +

CAT

- +

Yes, the string cat is upper cased to become CAT. - +

@@ -108,33 +108,33 @@ print(alist[2][0])

56

- +

Indexes start with 0, not 1. - +

c

- +

Yes, the first character of the string at index 2 is c - +

cat

- +

cat is the item at index 2 but then we index into it further. - +

Error, you cannot have two index values unless you are using slicing.

- +

Using more than one index is fine. You read it from left to right. - +

diff --git a/pretext/Lists/Aliasing.ptx b/pretext/Lists/Aliasing.ptx index 415797a7e..9752f2083 100644 --- a/pretext/Lists/Aliasing.ptx +++ b/pretext/Lists/Aliasing.ptx @@ -57,17 +57,17 @@ print(alist)

[4, 2, 8, 6, 5]

- +

blist is not a copy of alist, it is a reference to the list alist refers to. - +

[4, 2, 8, 999, 5]

- +

Yes, since alist and blist both reference the same list, changes to one also change the other. - +

@@ -88,41 +88,41 @@ list1=[1,100,1000] list2=[1,100,1000] list3=list1

print(list1 == list2)

- +

list1 and list2 have the same value - +

print(list1 is list2)

- +

list1 and list2 have the same value but do not refer to the same object - +

print(list1 is list3)

- +

list3 is assigned to list1, so they refer to the same object - +

print(list2 is not list3)

- +

list2 and list3 do not refer to the same object. - +

print(list2 != list3)

- +

list2 and list3 have the same value - +

diff --git a/pretext/Lists/AppendversusConcatenate.ptx b/pretext/Lists/AppendversusConcatenate.ptx index 85d82a24c..aaaade341 100644 --- a/pretext/Lists/AppendversusConcatenate.ptx +++ b/pretext/Lists/AppendversusConcatenate.ptx @@ -50,17 +50,17 @@ print(alist)

[4, 2, 8, 6, 5, 999]

- +

You cannot concatenate a list with an integer. - +

Error, you cannot concatenate a list with an integer.

- +

Yes, in order to perform concatenation you would need to write alist+[999]. You must have two lists. - +

diff --git a/pretext/Lists/ConcatenationandRepetition.ptx b/pretext/Lists/ConcatenationandRepetition.ptx index bf5a5effa..e128ec7a4 100644 --- a/pretext/Lists/ConcatenationandRepetition.ptx +++ b/pretext/Lists/ConcatenationandRepetition.ptx @@ -53,33 +53,33 @@ print(alist + blist)

6

- +

Concatenation does not add the lengths of the lists. - +

[1, 2, 3, 4, 5, 6]

- +

Concatenation does not reorder the items. - +

[1, 3, 5, 2, 4, 6]

- +

Yes, a new list with all the items of the first list followed by all those from the second. - +

[3, 7, 11]

- +

Concatenation does not add the individual items. - +

@@ -98,33 +98,33 @@ print(alist * 3)

9

- +

Repetition does not multiply the lengths of the lists. It repeats the items. - +

[1, 1, 1, 3, 3, 3, 5, 5, 5]

- +

Repetition does not repeat each item individually. - +

[1, 3, 5, 1, 3, 5, 1, 3, 5]

- +

Yes, the items of the list are repeated 3 times, one after another. - +

[3, 9, 15]

- +

Repetition does not multiply the individual items. - +

diff --git a/pretext/Lists/ListComprehensions.ptx b/pretext/Lists/ListComprehensions.ptx index f2ac50d45..c496a392e 100644 --- a/pretext/Lists/ListComprehensions.ptx +++ b/pretext/Lists/ListComprehensions.ptx @@ -52,33 +52,33 @@ print(blist)

[4,2,8,6,5]

- +

Items from alist are doubled before being placed in blist. - +

[8,4,16,12,10]

- +

Not all the items in alist are to be included in blist. Look at the if clause. - +

10

- +

The result needs to be a list. - +

[10].

- +

Yes, 5 is the only odd number in alist. It is doubled before being placed in blist. - +

diff --git a/pretext/Lists/ListLength.ptx b/pretext/Lists/ListLength.ptx index c7e1ca7fb..3efce0bba 100644 --- a/pretext/Lists/ListLength.ptx +++ b/pretext/Lists/ListLength.ptx @@ -30,17 +30,17 @@ print(len(alist))

4

- +

len returns the actual number of items in the list, not the maximum index value. - +

5

- +

Yes, there are 5 items in this list. - +

@@ -59,17 +59,17 @@ print(len(alist))

7

- +

Yes, there are 7 items in this list even though two of them happen to also be lists. - +

8

- +

len returns the number of top level items in the list. It does not count items in sublists. - +

diff --git a/pretext/Lists/ListMembership.ptx b/pretext/Lists/ListMembership.ptx index 5a3390b01..b29058762 100644 --- a/pretext/Lists/ListMembership.ptx +++ b/pretext/Lists/ListMembership.ptx @@ -29,17 +29,17 @@ print(3.14 in alist)

True

- +

Yes, 3.14 is an item in the list alist. - +

False

- +

There are 7 items in the list, 3.14 is one of them. - +

@@ -58,17 +58,17 @@ print(57 in alist)

True

- +

in returns True for top level items only. 57 is in a sublist. - +

False

- +

Yes, 57 is not a top level item in alist. It is in a sublist. - +

diff --git a/pretext/Lists/ListMethods.ptx b/pretext/Lists/ListMethods.ptx index 263c05652..a85757213 100644 --- a/pretext/Lists/ListMethods.ptx +++ b/pretext/Lists/ListMethods.ptx @@ -225,25 +225,25 @@ print(alist)

[4, 2, 8, 6, 5, False, True]

- +

True was added first, then False was added last. - +

[4, 2, 8, 6, 5, True, False]

- +

Yes, each item is added to the end of the list. - +

[True, False, 4, 2, 8, 6, 5]

- +

append adds at the end, not the beginning. - +

@@ -264,25 +264,25 @@ print(alist)

[False, 4, 2, True, 8, 6, 5]

- +

Yes, first True was added at index 2, then False was added at index 0. - +

[4, False, True, 2, 8, 6, 5]

- +

insert will place items at the index position specified and move everything down to the right. - +

[False, 2, True, 6, 5]

- +

insert does not remove anything or replace anything. - +

@@ -303,25 +303,25 @@ print(alist)

[4, 8, 6]

- +

pop(2) removes the item at index 2, not the 2 itself. - +

[2, 6, 5]

- +

pop() removes the last item, not the first. - +

[4, 2, 6]

- +

Yes, first the 8 was removed, then the last item, which was 5. - +

@@ -341,33 +341,33 @@ print(alist)

[2, 8, 6, 5]

- +

alist is now the value that was returned from pop(0). - +

[4, 2, 8, 6, 5]

- +

pop(0) changes the list by removing the first item. - +

4

- +

Yes, first the 4 was removed from the list, then returned and assigned to alist. The list is lost. - +

None

- +

pop(0) returns the first item in the list so alist has now been changed. - +

diff --git a/pretext/Lists/ListSlices.ptx b/pretext/Lists/ListSlices.ptx index 44ad1b42e..6b13a88d8 100644 --- a/pretext/Lists/ListSlices.ptx +++ b/pretext/Lists/ListSlices.ptx @@ -32,25 +32,25 @@ print(alist[4:])

[ [ ], 3.14, False]

- +

Yes, the slice starts at index 4 and goes up to and including the last item. - +

[ [ ], 3.14]

- +

By leaving out the upper bound on the slice, we go up to and including the last item. - +

[ [56, 57, "dog"], [ ], 3.14, False]

- +

Index values start at 0. - +

diff --git a/pretext/Lists/ListValues.ptx b/pretext/Lists/ListValues.ptx index b30b2b489..221220d3d 100644 --- a/pretext/Lists/ListValues.ptx +++ b/pretext/Lists/ListValues.ptx @@ -47,17 +47,17 @@ print(newlist)

False

- +

Yes, unlike strings, lists can consist of any type of Python data. - +

True

- +

Lists are heterogeneous, meaning they can have different types of data. - +

diff --git a/pretext/Lists/Listsandforloops.ptx b/pretext/Lists/Listsandforloops.ptx index 0d84e038d..e32f80414 100644 --- a/pretext/Lists/Listsandforloops.ptx +++ b/pretext/Lists/Listsandforloops.ptx @@ -71,33 +71,33 @@ print(blist)

[4, 2, 8, 6, 5]

- +

5 is added to each item before the append is peformed. - +

[4, 2, 8, 6, 5, 5]

- +

There are too many items in this list. Only 5 append operations are performed. - +

[9, 7, 13, 11, 10]

- +

Yes, the for loop processes each item of the list. 5 is added before it is appended to blist. - +

Error, you cannot concatenate inside an append.

- +

5 is added to each item before the append is performed. - +

diff --git a/pretext/Lists/ListsareMutable.ptx b/pretext/Lists/ListsareMutable.ptx index d300efedf..7ed992aec 100644 --- a/pretext/Lists/ListsareMutable.ptx +++ b/pretext/Lists/ListsareMutable.ptx @@ -72,25 +72,25 @@ print(alist)

[4, 2, True, 8, 6, 5]

- +

Item assignment does not insert the new item into the list. - +

[4, 2, True, 6, 5]

- +

Yes, the value True is placed in the list at index 2. It replaces 8. - +

Error, it is illegal to assign

- +

Item assignment is allowed with lists. Lists are mutable. - +

diff --git a/pretext/Lists/NestedLists.ptx b/pretext/Lists/NestedLists.ptx index 04ea9df74..cfbb7cc20 100644 --- a/pretext/Lists/NestedLists.ptx +++ b/pretext/Lists/NestedLists.ptx @@ -39,33 +39,33 @@ else:

6

- +

6 is in the wrong list. alist[1] refers to the second item in alist, namely [888,999]. - +

8

- +

8 is in the wrong list. alist[1] refers to the second item in alist, namely [888,999]. - +

888

- +

Yes, alist[0][1][0] is True and alist[1] is the second list, the first item is 888. - +

999

- +

alist[0][1][0] is True. Take another look at the if statement. - +

diff --git a/pretext/Lists/RepetitionandReferences.ptx b/pretext/Lists/RepetitionandReferences.ptx index 7e5bdaf90..05dfc4884 100644 --- a/pretext/Lists/RepetitionandReferences.ptx +++ b/pretext/Lists/RepetitionandReferences.ptx @@ -77,25 +77,25 @@ print(alist)

[4, 2, 8, 999, 5, 4, 2, 8, 6, 5]

- +

print(alist) not print(blist) - +

[4, 2, 8, 999, 5]

- +

blist is changed, not alist. - +

[4, 2, 8, 6, 5]

- +

Yes, alist was unchanged by the assignment statement. blist was a copy of the references in alist. - +

@@ -116,33 +116,33 @@ print(blist)

[4, 2, 8, 999, 5, 4, 2, 8, 999, 5]

- +

[alist] * 2 creates a list containing alist repeated 2 times - +

[[4, 2, 8, 999, 5], [4, 2, 8, 999, 5]]

- +

Yes, blist contains two references, both to alist. - +

[4, 2, 8, 6, 5]

- +

print(blist) - +

[[4, 2, 8, 999, 5], [4, 2, 8, 6, 5]]

- +

blist contains two references, both to alist so changes to alist appear both times. - +

diff --git a/pretext/Lists/StringsandLists.ptx b/pretext/Lists/StringsandLists.ptx index dbe5d3d00..92b6c567d 100644 --- a/pretext/Lists/StringsandLists.ptx +++ b/pretext/Lists/StringsandLists.ptx @@ -62,33 +62,33 @@ print(init)

Poe

- +

Three characters but not the right ones. namelist is the list of names. - +

EdgarAllanPoe

- +

Too many characters in this case. There should be a single letter from each name. - +

EAP

- +

Yes, split creates a list of the three names. The for loop iterates through the names and creates a string from the first characters. - +

William Shakespeare

- +

That does not make any sense. - +

diff --git a/pretext/Lists/TheAccumulatorPatternwithLists.ptx b/pretext/Lists/TheAccumulatorPatternwithLists.ptx index 17462b8a3..6b528b1b1 100644 --- a/pretext/Lists/TheAccumulatorPatternwithLists.ptx +++ b/pretext/Lists/TheAccumulatorPatternwithLists.ptx @@ -185,33 +185,33 @@ print(x)

2

- +

Though only two of the letters in the list are found, we count them each time they appear. - +

5

- +

Yes, we add to x each time we come across a letter in the list. - +

0

- +

Check again what the conditional is evaluating. The value of i will be a character in the string s, so what will happen in the if statement? - +

There is an error in the code so it cannot run.

- +

There are no errors in this code. - +

@@ -234,33 +234,33 @@ print(min_value)

10

- +

Not quite. What is the conditional checking? - +

1

- +

min_value was set to a number that was smaller than any of the numbers in the list, so it was never updated in the for loop. - +

0

- +

Yes, min_value was set to a number that was smaller than any of the numbers in the list, so it was never updated in the for loop. - +

There is an error in the code so it cannot run.

- +

The code does not have an error that would prevent it from running. - +

diff --git a/pretext/MoreAboutIteration/2DimensionalIterationImageProcessing.ptx b/pretext/MoreAboutIteration/2DimensionalIterationImageProcessing.ptx index 8e2da7033..4d2e0753f 100644 --- a/pretext/MoreAboutIteration/2DimensionalIterationImageProcessing.ptx +++ b/pretext/MoreAboutIteration/2DimensionalIterationImageProcessing.ptx @@ -271,33 +271,33 @@ print(p.getGreen(), p.getBlue())

Dark red

- +

Because all three values are close to 0, the color will be dark. But because the red value is higher than the other two, the color will appear red. - +

Light red

- +

The closer the values are to 0, the darker the color will appear. - +

Dark green

- +

The first value in RGB is the red value. The second is the green. This color has no green in it. - +

Light green

- +

The first value in RGB is the red value. The second is the green. This color has no green in it. - +

@@ -429,33 +429,33 @@ print(p.getRed(), p.getGreen(), p.getBlue())

149 132 122

- +

These are the values for the pixel at row 30, column 100. Get the values for row 100 and column 30 with p = img.getPixel(100, 30). - +

183 179 170

- +

Yes, the RGB values are 183 179 170 at row 100 and column 30. - +

165 161 158

- +

These are the values from the original example (row 45, column 55). Get the values for row 100 and column 30 with p = img.getPixel(30, 100). - +

201 104 115

- +

These are simply made-up values that may or may not appear in the image. Get the values for row 100 and column 30 with p = img.getPixel(30, 100). - +

@@ -622,33 +622,33 @@ d.

Output a

- +

i will start with a value of 0 and then j will iterate from 0 to 1. Next, i will be 1 and j will iterate from 0 to 1. Finally, i will be 2 and j will iterate from 0 to 1. - +

Output b

- +

The inner for-loop controls the second digit (j). The inner for-loop must complete before the outer for-loop advances. - +

Output c

- +

The inner for-loop controls the second digit (j). Notice that the inner for-loop is over the list [0, 1]. - +

Output d

- +

The outer for-loop runs 3 times (0, 1, 2) and the inner for-loop runs twice for each time the outer for-loop runs, so this code prints exactly 6 lines. - +

@@ -676,33 +676,33 @@ newblue = 0

It would look like a red-washed version of the bell image

- +

Because we are removing the green and the blue values, but keeping the variation of the red the same, you will get the same image, but it will look like it has been bathed in red. - +

It would be a solid red rectangle the same size as the original image

- +

Because the red value varies from pixel to pixel, this will not look like a solid red rectangle. For it to look like a solid red rectangle each pixel would have to have exactly the same red value. - +

It would look the same as the original image

- +

If you remove the blue and green values from the pixels, the image will look different, even though there does not appear to be any blue or green in the original image (remember that other colors are made of combinations of red, green and blue). - +

It would look the same as the negative image in the example code

- +

Because we have changed the value of the pixels from what they were in the original ActiveCode box code, the image will not be the same. - +

diff --git a/pretext/MoreAboutIteration/RandomlyWalkingTurtles.ptx b/pretext/MoreAboutIteration/RandomlyWalkingTurtles.ptx index 992151fcd..4447f8c5e 100644 --- a/pretext/MoreAboutIteration/RandomlyWalkingTurtles.ptx +++ b/pretext/MoreAboutIteration/RandomlyWalkingTurtles.ptx @@ -184,25 +184,25 @@ wn.exitonclick()

a for-loop or a while-loop

- +

Although you do not know how many iterations you loop will run before the program starts running, once you have chosen your random integer, Python knows exactly how many iterations the loop will run, so either a for-loop or a while-loop will work. - +

only a for-loop

- +

As you learned in section 7.2, a while-loop can always be used for anything a for-loop can be used for. - +

only a while-loop

- +

Although you do not know how many iterations you loop will run before the program starts running, once you have chosen your random integer, Python knows exactly how many iterations the loop will run, so this is an example of definite iteration. - +

@@ -215,33 +215,33 @@ wn.exitonclick()

Returns True if the turtle is still on the screen and False if the turtle is no longer on the screen.

- +

The isInScreen function computes the boolean test of whether the turtle is still in the window. It makes the condition of the while loop in the main part of the code simpler. - +

Uses a while loop to move the turtle randomly until it goes off the screen.

- +

The isInScreen function does not contain a while-loop. That loop is outside the isInScreen function. - +

Turns the turtle right or left at random and moves the turtle forward 50.

- +

The isInScreen function does not move the turtle. - +

Calculates and returns the position of the turtle in the window.

- +

While the isInScreen function does use the size of the window and position of the turtle, it does not return the turtle position. - +

diff --git a/pretext/MoreAboutIteration/SentinelValuesAndValidation.ptx b/pretext/MoreAboutIteration/SentinelValuesAndValidation.ptx index 8c7cca24d..1e634eb8b 100644 --- a/pretext/MoreAboutIteration/SentinelValuesAndValidation.ptx +++ b/pretext/MoreAboutIteration/SentinelValuesAndValidation.ptx @@ -103,17 +103,17 @@ checkout()

True

- +

Keep in mind there are multiple ways to stop a while loop, such as a sentinal value. - +

False

- +

While loops do not have a set number of times they will iterate, so they will continue until something breaks the loop. Keep this in mind while coding with while loops. - +

diff --git a/pretext/MoreAboutIteration/SimpleTables.ptx b/pretext/MoreAboutIteration/SimpleTables.ptx index 150d58408..ee9d41b4a 100644 --- a/pretext/MoreAboutIteration/SimpleTables.ptx +++ b/pretext/MoreAboutIteration/SimpleTables.ptx @@ -54,33 +54,33 @@ for x in range(13): # generate values for columns

A tab will line up items in a second column, regardless of how many characters were in the first column, while spaces will not.

- +

Assuming the size of the first column is less than the size of the tab width. - +

There is no difference

- +

Tabs and spaces will sometimes make output appear visually different. - +

A tab is wider than a sequence of spaces

- +

A tab has a pre-defined width that is equal to a given number of spaces. - +

You must use tabs for creating tables. You cannot use spaces.

- +

You may use spaces to create tables. The columns might look jagged, or they might not, depending on the width of the items in each column. - +

diff --git a/pretext/MoreAboutIteration/The3n1Sequence.ptx b/pretext/MoreAboutIteration/The3n1Sequence.ptx index 1b7602f41..f2010b027 100644 --- a/pretext/MoreAboutIteration/The3n1Sequence.ptx +++ b/pretext/MoreAboutIteration/The3n1Sequence.ptx @@ -85,25 +85,25 @@ seq3np1(3)

Yes.

- +

The 3n+1 sequence has not been proven to terminate for all values of n. - +

No.

- +

It has not been disproven that the 3n+1 sequence will terminate for all values of n. In other words, there might be some value for n such that this sequence does not terminate. We just have not found it yet. - +

No one knows.

- +

That this sequence terminates for all values of n has not been proven or disproven so no one knows whether the while loop will always terminate or not. - +

diff --git a/pretext/MoreAboutIteration/Theforlooprevisited.ptx b/pretext/MoreAboutIteration/Theforlooprevisited.ptx index 705cb8832..38c8c54ab 100644 --- a/pretext/MoreAboutIteration/Theforlooprevisited.ptx +++ b/pretext/MoreAboutIteration/Theforlooprevisited.ptx @@ -53,41 +53,41 @@ printnums(5, 3)

5

- +

This is how many times the inner loop will print for each iteration of the outer loop. - +

8

- +

Keep in mind that the inner loop is part of the body of the outer loop. - +

15

- +

The inner loop will print a total of 15 times; however the outer loop is also printing the same phrase. - +

18

- +

Correct! The nested loop will be run 3 times, making a total of 18. - +

20

- +

Pay attention to the order of x and y - +

diff --git a/pretext/MoreAboutIteration/ThewhileStatement.ptx b/pretext/MoreAboutIteration/ThewhileStatement.ptx index 1a9d87cf7..41cb652e4 100644 --- a/pretext/MoreAboutIteration/ThewhileStatement.ptx +++ b/pretext/MoreAboutIteration/ThewhileStatement.ptx @@ -124,17 +124,17 @@ print(sumTo(4))

True

- +

Although the while loop uses a different syntax, it is just as powerful as a for-loop and often more flexible. - +

False

- +

Often a for-loop is more natural and convenient for a task, but that same task can always be expressed using a while loop. - +

@@ -157,33 +157,33 @@ print(answer)

n starts at 10 and is incremented by 1 each time through the loop, so it will always be positive

- +

The loop will run as long as n is positive. In this case, we can see that n will never become non-positive. - +

answer starts at 1 and is incremented by n each time, so it will always be positive

- +

While it is true that answer will always be positive, answer is not considered in the loop condition. - +

You cannot compare n to 0 in while loop. You must compare it to another variable.

- +

It is perfectly valid to compare n to 0. Though indirectly, this is what causes the infinite loop. - +

In the while loop body, we must set n to False, and this code does not do that.

- +

The loop condition must become False for the loop to terminate, but n by itself is not the condition in this case. - +

@@ -208,25 +208,25 @@ print(n, x)

4 7

- +

Setting a variable so the loop condition would be false in the middle of the loop body does not keep the variable from actually being set. - +

5 7

- +

Setting a variable so the loop condition would be false in the middle of the loop body does not stop execution of statements in the rest of the loop body. - +

7 15

- +

After n becomes 5 and the test would be False, but the test does not actually come until after the end of the loop - only then stopping execution of the repetition of the loop. - +

diff --git a/pretext/PythonModules/CreatingModules.ptx b/pretext/PythonModules/CreatingModules.ptx index a62e7566a..617bf02d5 100644 --- a/pretext/PythonModules/CreatingModules.ptx +++ b/pretext/PythonModules/CreatingModules.ptx @@ -30,33 +30,33 @@ coffee_roasts = ["hot chocolate", "light", "medium", "dark", "espresso"]

the code inside a function

- +

The code inside a function is called a function body. - +

a file containing Python code

- +

Python modules are just Python source code files. - +

the comments before a function

- +

The comments just before a function are the function documentation. - +

a small block of Python code

- +

Modules may be small, but there is a more accurate answer. - +

@@ -90,33 +90,33 @@ print("Available roasts:", coffee_shop.coffee_roasts)

writing a new function or class

- +

You may write a function or class inside a module, but this alone does not create one. - +

placing an import statement at the top of a file

- +

Import statements permit you to use other modules, but they do not create one. - +

placing code in a Python file in the same directory as your other source code

- +

Modules are Python source code files. - +

creating a comment block at the beginning of a file

- +

Modules should include comment blocks at the top, but writing a comment block at the top of a file does not create a new module. - +

@@ -191,33 +191,33 @@ print(shop_says)

the first variable name in the module

- +

This does not determine module name, and not all modules export variables. - +

a comment early in the module

- +

This does not determine module name, and comments are not mandatory components of modules. - +

it's called whatever we name it in the "import" statement

- +

The import statement uses the module name to lookup the correct module, and an import statement is not used to create the module. - +

the filename of the module

- +

The filename of the module determines the name of the import. - +

diff --git a/pretext/PythonModules/Themathmodule.ptx b/pretext/PythonModules/Themathmodule.ptx index e1d0b57e3..d534a7bc2 100644 --- a/pretext/PythonModules/Themathmodule.ptx +++ b/pretext/PythonModules/Themathmodule.ptx @@ -47,33 +47,33 @@ print(math.sin(math.radians(90))) # sin of 90 degrees

import math

- +

The module must be imported before you can use anything declared inside the module. - +

include math

- +

The correct term is not include, but you are close. - +

use math

- +

You will be using parts of the module, but that is not the right term. - +

You don't need a statement. You can always use the math module

- +

You cannot use a Python module without a statement at the top of your program that explicitly tells Python you want to use the module. - +

diff --git a/pretext/PythonModules/Therandommodule.ptx b/pretext/PythonModules/Therandommodule.ptx index 2b3113dd3..0a1a100fb 100644 --- a/pretext/PythonModules/Therandommodule.ptx +++ b/pretext/PythonModules/Therandommodule.ptx @@ -89,33 +89,33 @@ print(result)

math.pi

- +

To invoke or reference something contained in a module you use the dot (.) notation. - +

math(pi)

- +

This is the syntax for calling a function, not referencing an item in a module. - +

pi.math

- +

The module name must come first when accessing values and functions with a module. - +

math->pi

- +

The -> notation is not used in Python. - +

@@ -128,33 +128,33 @@ print(result)

the math module

- +

While you might want to use the math module for other numerical computations in your program, it does not contain functions that are likely to help you simulate a dice roll. - +

the random module

- +

You would likely call the function random.randrange. - +

the turtle module

- +

The turtle module, while producing interesting graphics, is unlikely to help you here. - +

the game module

- +

Python does not have a game module. - +

@@ -167,33 +167,33 @@ print(result)

prob = random.randrange(1, 101)

- +

This will generate a number between 1 and 101, but does not include 101. - +

prob = random.randrange(1, 100)

- +

This will generate a number between 1 and 100, but does not include 100. The highest value generated will be 99. - +

prob = random.randrange(0, 101)

- +

This will generate a number between 0 and 100. The lowest value generated is 0. The highest value generated will be 100. - +

prob = random.randrange(0, 100)

- +

This will generate a number between 0 and 100, but does not include 100. The lowest value generated is 0 and the highest value generated will be 99. - +

@@ -206,33 +206,33 @@ print(result)

There is no computer on the stage for the drawing.

- +

They could easily put one there. - +

Because computers don't really generate random numbers, they generate pseudo-random numbers.

- +

Computers generate random numbers using a deterministic algorithm. This means that if anyone ever found out the algorithm they could accurately predict the next value to be generated and would always win the lottery. - +

They would just generate the same numbers over and over again.

- +

This might happen if the same seed value was used over and over again, but they could make sure this was not the case. - +

The computer can't tell what values were already selected, so it might generate all 5's instead of 5 unique numbers.

- +

While a programmer would need to ensure the computer did not select the same number more than once, it is easy to ensure this. - +

diff --git a/pretext/PythonModules/modules.ptx b/pretext/PythonModules/modules.ptx index a18b44559..eb1afb7a0 100644 --- a/pretext/PythonModules/modules.ptx +++ b/pretext/PythonModules/modules.ptx @@ -62,33 +62,33 @@ wn.exitonclick()

A file containing Python definitions and statements intended for use in other Python programs.

- +

A module can be reused in different programs. - +

A separate block of code within a program.

- +

While a module is separate block of code, it is separate from a program. - +

One line of code in a program.

- +

The call to a feature within a module may be one line of code, but modules are usually multiple lines of code separate from the program - +

A file that contains documentation about functions in Python.

- +

Each module has its own documentation, but the module itself is more than just documentation. - +

@@ -101,33 +101,33 @@ wn.exitonclick()

Go to the Python Documentation site.

- +

The site contains a listing of all the standard modules that are available with Python. - +

Look at the import statements of the program you are working with or writing.

- +

The import statements only tell you what modules are currently being used in the program, not how to use them or what they contain. - +

Ask the professor

- +

While the professor knows a subset of the modules available in Python, chances are the professor will have to look up the available modules just like you would. - +

Look in this textbook.

- +

This book only explains a portion of the modules available. For a full listing you should look elsewhere. - +

@@ -140,17 +140,17 @@ wn.exitonclick()

True

- +

Only turtle, math, and random have been ported to work in activecode at this time. - +

False

- +

Only turtle, math, and random have been ported to work in activecode at this time. - +

diff --git a/pretext/PythonTurtle/InstancesAHerdofTurtles.ptx b/pretext/PythonTurtle/InstancesAHerdofTurtles.ptx index 08aab6962..c2460bc95 100644 --- a/pretext/PythonTurtle/InstancesAHerdofTurtles.ptx +++ b/pretext/PythonTurtle/InstancesAHerdofTurtles.ptx @@ -89,17 +89,17 @@ wn.exitonclick()

True

- +

You can create and use as many turtles as you like. As long as they have different names, you can operate them independently, and make them move in any order you like. To convince yourself this is true, try interleaving the instructions for alex and tess in ActiveCode box 3. - +

False

- +

You can create and use as many turtles as you like. As long as they have different names, you can operate them independently, and make them move in any order you like. If you are not totally convinced, try interleaving the instructions for alex and tess in ActiveCode box 3. - +

diff --git a/pretext/PythonTurtle/IterationSimplifiesourTurtleProgram.ptx b/pretext/PythonTurtle/IterationSimplifiesourTurtleProgram.ptx index 8668d9e34..c6870f7eb 100644 --- a/pretext/PythonTurtle/IterationSimplifiesourTurtleProgram.ptx +++ b/pretext/PythonTurtle/IterationSimplifiesourTurtleProgram.ptx @@ -149,33 +149,33 @@ for number in [5, 4, 3, 2, 1, 0]:

1

- +

The loop body prints one line, but the body will execute exactly one time for each element in the list [5, 4, 3, 2, 1, 0]. - +

5

- +

Although the biggest number in the list is 5, there are actually 6 elements in the list. - +

6

- +

The loop body will execute (and print one line) for each of the 6 elements in the list [5, 4, 3, 2, 1, 0]. - +

10

- +

The loop body will not execute more times than the number of elements in the list. - +

@@ -188,25 +188,25 @@ for number in [5, 4, 3, 2, 1, 0]:

They are indented to the same degree from the loop header.

- +

The loop body can have any number of lines, all indented from the loop header. - +

There is always exactly one line in the loop body.

- +

The loop body may have more than one line. - +

The loop body ends with a semi-colon (;) which is not shown in the code above.

- +

Python does not need semi-colons in its syntax, but relies mainly on indentation. - +

@@ -225,33 +225,33 @@ for number in [5, 4, 3, 2, 1, 0]:

2

- +

Python gives number the value of items in the list, one at a time, in order (from left to right). number gets a new value each time the loop repeats. - +

4

- +

Yes, Python will process the items from left to right so the first time the value of number is 5 and the second time it is 4. - +

5

- +

Python gives number the value of items in the list, one at a time, in order. number gets a new value each time the loop repeats. - +

1

- +

Python gives number the value of items in the list, one at a time, in order (from left to right). number gets a new value each time the loop repeats. - +

@@ -272,25 +272,25 @@ for aColor in ["yellow", "red", "green", "blue"]:

Draw a square using the same color for each side.

- +

The question is not asking you to describe the outcome of the entire loop, the question is asking you about the outcome of a **single iteration** of the loop. - +

Draw a square using a different color for each side.

- +

Notice that aColor is never actually used inside the loop. - +

Draw one side of a square.

- +

The body of the loop only draws one side of the square. It will be repeated once for each item in the list. However, the color of the turtle never changes. - +

diff --git a/pretext/PythonTurtle/OurFirstTurtleProgram.ptx b/pretext/PythonTurtle/OurFirstTurtleProgram.ptx index 037797cec..49bba72b1 100644 --- a/pretext/PythonTurtle/OurFirstTurtleProgram.ptx +++ b/pretext/PythonTurtle/OurFirstTurtleProgram.ptx @@ -57,33 +57,33 @@ alex.forward(75) # complete the second side of a rectangle

North

- +

Some turtle systems start with the turtle facing north, but not this one. - +

South

- +

No, look at the first example with a turtle. Which direction does the turtle move? - +

East

- +

Yes, the turtle starts out facing east. - +

West

- +

No, look at the first example with a turtle. Which direction does the turtle move? - +

@@ -241,33 +241,33 @@ alex.forward(75)

It creates a new turtle object that can be used for drawing.

- +

The line "alex = turtle.Turtle()" is what actually creates the turtle object. - +

It defines the module turtle which will allow you to create a Turtle object and draw with it.

- +

This line imports the module called turtle, which has all the built in functions for drawing on the screen with the Turtle object. - +

It makes the turtle draw half of a rectangle on the screen.

- +

This functionality is performed with the lines: "alex.forward(150)", "lex.left(90)", and "alex.forward(75)" - +

Nothing, it is unnecessary.

- +

If we leave it out, Python will give an error saying that it does not know about the name "turtle" when it reaches the line "wn = turtle.Screen()" - +

@@ -280,25 +280,25 @@ alex.forward(75)

This is simply for clarity. It would also work to just type "Turtle()" instead of "turtle.Turtle()".

- +

We must specify the name of the module where Python can find the Turtle object. - +

The period (.) is what tells Python that we want to invoke a new object.

- +

The period separates the module name from the object name. The parentheses at the end are what tell Python to invoke a new object. - +

The first "turtle" (before the period) tells Python that we are referring to the turtle module, which is where the object "Turtle" is found.

- +

Yes, the Turtle type is defined in the module turtle. Remember that Python is case sensitive and Turtle is different from turtle. - +

@@ -311,17 +311,17 @@ alex.forward(75)

True

- +

In this chapter you saw one named alex and one named tess, but any legal variable name is allowed. - +

False

- +

A variable, including one referring to a Turtle object, can have whatever name you choose as long as it follows the naming conventions from Chapter 2. - +

@@ -337,9 +337,9 @@ alex.forward(75) right turn of 90 degrees before drawing, draw a line 150 pixels long, turn left 90, and draw a line 75 pixels long

- +

This code would turn the turtle to the south before drawing - +

@@ -347,9 +347,9 @@ alex.forward(75) left turn of 180 degrees before drawing,  draw a line 150 pixels long, turn left 90, and draw a line 75 pixels long

- +

This code would turn the turtle to the west before drawing - +

@@ -357,9 +357,9 @@ alex.forward(75) left turn of 270 degrees before drawing,  draw a line 150 pixels long, turn left 90, and draw a line 75 pixels long

- +

This code would turn the turtle to the south before drawing - +

@@ -367,9 +367,9 @@ alex.forward(75) right turn of 270 degrees before drawing, draw a line 150 pixels long, turn right 90, and draw a line 75 pixels long

- +

This code is almost correct, but the short end would be facing east instead of west. - +

@@ -377,9 +377,9 @@ alex.forward(75) left turn of 90 degrees before drawing,  draw a line 150 pixels long, turn left 90, and draw a line 75 pixels long

- +

Yes, the turtle starts facing east, so to turn it north you can turn left 90 or right 270 degrees. - +

diff --git a/pretext/PythonTurtle/TherangeFunction.ptx b/pretext/PythonTurtle/TherangeFunction.ptx index 1c79cedd4..70cbf61ab 100644 --- a/pretext/PythonTurtle/TherangeFunction.ptx +++ b/pretext/PythonTurtle/TherangeFunction.ptx @@ -118,33 +118,33 @@ for i in range(0, 20, 2):

Range should generate a sequence that stops before 10 (including 9).

- +

Range will generate the sequence 3, 5, 7, 9. - +

Range should generate a sequence that starts at 10 (including 10).

- +

The first argument (3) tells range what number to start at. - +

Range should generate a sequence starting at 3 that stops at 10 (including 10).

- +

Range will always stop at the number in the sequence before (not including) the specified limit for the sequence. - +

Range should generate a sequence using every 10th number between the start and the stopping number.

- +

The third argument (2) tells range how many numbers to skip between each element in the sequence. - +

@@ -157,33 +157,33 @@ for i in range(0, 20, 2):

range(2, 5, 8)

- +

This command generates the sequence with just the number 2 because the first parameter (2) tells range where to start, the second number tells range where to end (before 5) and the third number tells range how many numbers to skip between elements (8). Since 10 >= 5, there is only one number in this sequence. - +

range(2, 8, 3)

- +

This command generates the sequence 2, 5 because 8 is not less than 8 (the specified number past the end). - +

range(2, 10, 3)

- +

The first number is the starting point, the second is past the last allowed, and the third is the amount to increment by. - +

range(8, 1, -3)

- +

This command generates the sequence 8, 5, 2 because it starts at 8, ends before 1, and skips to every third number going down. - +

@@ -196,33 +196,33 @@ for i in range(0, 20, 2):

It will generate a sequence starting at 0, with every number included up to but not including the argument it was passed.

- +

Yes, if you only give one number to range it starts with 0 and ends before the number specified incrementing by 1. - +

It will generate a sequence starting at 1, with every number up to but not including the argument it was passed.

- +

Range with one parameter starts at 0. - +

It will generate a sequence starting at 1, with every number including the argument it was passed.

- +

Range with one parameter starts at 0, and never includes the argument it was passed. - +

It will cause an error: range always takes exactly 3 arguments.

- +

If range is passed only one argument, it interprets that argument as one past the end of the list. - +

@@ -235,33 +235,33 @@ for i in range(0, 20, 2):

range(5, 25, 5)

- +

The step 5 is positive, while the given sequence is decreasing. This answer creates the reversed, increasing sequence. - +

range(20, 3, -5)

- +

Yes: If we take steps of -5, not worrying about the ending, we get 20, 15, 10, 5, 0, .... The limit 3 is past the 5, so the range sequence stops with the 5. - +

range(20, 5, 4)

- +

The step 5 is positive so the sequence would need to increase from 20 toward 4. That does not make sense and the sequence would be empty. - +

range(20, 5, -5)

- +

the sequence can never include the second parameter (5). The second parameter must always be past the end of the range sequence. - +

@@ -274,25 +274,25 @@ for i in range(0, 20, 2):

No other value would give the same sequence.

- +

The sequence produced has steps of 4: 2, 6, 10. The next would be 14, but it is not before the limit 12. There are other limit choices past 10, but not past 14. - +

The only other choice is 14.

- +

14 would work: It is also past 10, and not past 14, but there are other integers with the same properties. - +

11, 13, or 14

- +

Yes, any integer past 10, and not past the next step at 14 would work. - +

diff --git a/pretext/Selection/BooleanFunctions.ptx b/pretext/Selection/BooleanFunctions.ptx index 9acd737e2..ea0513684 100644 --- a/pretext/Selection/BooleanFunctions.ptx +++ b/pretext/Selection/BooleanFunctions.ptx @@ -83,25 +83,25 @@ else:

A function that returns True or False

- +

A Boolean function is just like any other function, but it always returns True or False. - +

A function that takes True or False as an argument

- +

A Boolean function may take any number of arguments (including 0, though that is rare), of any type. - +

The same as a Boolean expression

- +

A Boolean expression is a statement that evaluates to True or False, e.g. 5+3==8. A function is a series of expressions grouped together with a name that are only executed when you call the function. - +

@@ -119,17 +119,17 @@ return x + y < z

Yes

- +

It is perfectly valid to return the result of evaluating a Boolean expression. - +

No

- +

x +y < z is a valid Boolean expression, which will evaluate to True or False. It is perfectly legal to return True or False from a function, and to have the statement to be evaluated in the same line as the return keyword. - +

diff --git a/pretext/Selection/BooleanValuesandBooleanExpressions.ptx b/pretext/Selection/BooleanValuesandBooleanExpressions.ptx index 75dc1db61..12ee976c0 100644 --- a/pretext/Selection/BooleanValuesandBooleanExpressions.ptx +++ b/pretext/Selection/BooleanValuesandBooleanExpressions.ptx @@ -78,41 +78,41 @@ x <= y # x is less than or equal to y

True

- +

True and False are both Boolean literals. - +

3 == 4

- +

The comparison between two numbers via == results in either True or False (in this case False), both Boolean values. - +

3 + 4

- +

3 + 4 evaluates to 7, which is a number, not a Boolean value. - +

3 + 4 == 7

- +

3 + 4 evaluates to 7. 7 == 7 then evaluates to True, which is a Boolean value. - +

"False"

- +

With the double quotes surrounding it, False is interpreted as a string, not a Boolean value. If the quotes had not been included, False alone is in fact a Boolean value. - +

diff --git a/pretext/Selection/Chainedconditionals.ptx b/pretext/Selection/Chainedconditionals.ptx index 835b296d8..f857e60fa 100644 --- a/pretext/Selection/Chainedconditionals.ptx +++ b/pretext/Selection/Chainedconditionals.ptx @@ -108,41 +108,41 @@ else:

I only

- +

You can not use a Boolean expression after an else. - +

II only

- +

Yes, II will give the same result. - +

III only

- +

No, III will not give the same result. The first if statement will be true, but the second will be false, so the else part will execute. - +

II and III

- +

No, Although II is correct III will not give the same result. Try it. - +

I, II, and III

- +

No, in I you can not have a Boolean expression after an else. - +

@@ -165,25 +165,25 @@ else:

a

- +

While the value in x is less than the value in y (3 is less than 5) it is not less than the value in z (3 is not less than 2). - +

b

- +

The value in y is not less than the value in x (5 is not less than 3). - +

c

- +

Since the first two Boolean expressions are false the else will be executed. - +

diff --git a/pretext/Selection/ConditionalExecutionBinarySelection.ptx b/pretext/Selection/ConditionalExecutionBinarySelection.ptx index 68e3b156b..fb106c6cb 100644 --- a/pretext/Selection/ConditionalExecutionBinarySelection.ptx +++ b/pretext/Selection/ConditionalExecutionBinarySelection.ptx @@ -70,33 +70,33 @@ else:

Just one.

- +

Each block may also contain more than one. - +

Zero or more.

- +

Each block must contain at least one statement. - +

One or more.

- +

Yes, a block must contain at least one statement and can have many statements. - +

One or more, and each must contain the same number.

- +

The blocks may contain different numbers of statements. - +

@@ -117,33 +117,33 @@ else:

TRUE

- +

TRUE is printed by the if-block, which only executes if the conditional (in this case, 4+5 == 10) is true. In this case 5+4 is not equal to 10. - +

FALSE

- +

Since 4+5==10 evaluates to False, Python will skip over the if block and execute the statement in the else block. - +

TRUE on one line and FALSE on the next

- +

Python would never print both TRUE and FALSE because it will only execute one of the if-block or the else-block, but not both. - +

Nothing will be printed

- +

Python will always execute either the if-block (if the condition is true) or the else-block (if the condition is false). It would never skip over both blocks. - +

@@ -178,33 +178,33 @@ d.

Output a

- +

Although TRUE is printed after the if-else statement completes, both blocks within the if-else statement print something too. In this case, Python would have had to have skipped both blocks in the if-else statement, which it never would do. - +

Output b

- +

Because there is a TRUE printed after the if-else statement ends, Python will always print TRUE as the last statement. - +

Output c

- +

Python will print FALSE from within the else-block (because 5+4 does not equal 10), and then print TRUE after the if-else statement completes. - +

Output d

- +

To print these three lines, Python would have to execute both blocks in the if-else statement, which it can never do. - +

diff --git a/pretext/Selection/Logicaloperators.ptx b/pretext/Selection/Logicaloperators.ptx index 61a4c5da4..63f36ae44 100644 --- a/pretext/Selection/Logicaloperators.ptx +++ b/pretext/Selection/Logicaloperators.ptx @@ -244,25 +244,25 @@ print(n % 2 == 0 or n % 3 == 0)

x > 0 and < 5

- +

Each comparison must be between exactly two values. In this case the right-hand expression < 5 lacks a value on its left. - +

x > 0 or x < 5

- +

Although this is legal Python syntax, the expression is incorrect. It will evaluate to true for all numbers that are either greater than 0 or less than 5. Because all numbers are either greater than 0 or less than 5, this expression will always be True. - +

x > 0 and x < 5

- +

Yes, with an and keyword both expressions must be true so the number must be greater than 0 an less than 5 for this expression to be true. Although most other programming languages do not allow this mathematical syntax, in Python, you could also write 0 < x < 5. - +

@@ -276,33 +276,33 @@ print(n % 2 == 0 or n % 3 == 0)

A and B

- +

Both A and B need to be True in order for this to be True. - +

A or B

- +

Either A or B need to be True in order for this to be True. - +

not A

- +

A needs to be False in order for this to be True. - +

not B

- +

B needs to be False in order for this to be True. - +

diff --git a/pretext/Selection/Nestedconditionals.ptx b/pretext/Selection/Nestedconditionals.ptx index ba25f525d..814d9846a 100644 --- a/pretext/Selection/Nestedconditionals.ptx +++ b/pretext/Selection/Nestedconditionals.ptx @@ -79,17 +79,17 @@ else:

No

- +

This is a legal nested if-else statement. The inner if-else statement is contained completely within the body of the outer else-block. - +

Yes

- +

This is a legal nested if-else statement. The inner if-else statement is contained completely within the body of the outer else-block. - +

diff --git a/pretext/Selection/OmittingtheelseClauseUnarySelection.ptx b/pretext/Selection/OmittingtheelseClauseUnarySelection.ptx index 5ad91c115..903947149 100644 --- a/pretext/Selection/OmittingtheelseClauseUnarySelection.ptx +++ b/pretext/Selection/OmittingtheelseClauseUnarySelection.ptx @@ -48,33 +48,33 @@ The negative number -10 is not valid here

Output a

- +

Because -10 is less than 0, Python will execute the body of the if-statement here. - +

Output b

- +

Python executes the body of the if-block as well as the statement that follows the if-block. - +

Output c

- +

Python will also execute the statement that follows the if-block (because it is not enclosed in an else-block, but rather just a normal statement). - +

It will cause an error because every if must have an else clause.

- +

It is valid to have an if-block without a corresponding else-block (though you cannot have an else-block without a corresponding if-block). - +

@@ -98,17 +98,17 @@ else:

No

- +

Every else-block must have exactly one corresponding if-block. If you want to chain if-else statements together, you must use the else if construct, described in the chained conditionals section. - +

Yes

- +

This will cause an error because the second else-block is not attached to a corresponding if-block. - +

diff --git a/pretext/Selection/PrecedenceofOperators.ptx b/pretext/Selection/PrecedenceofOperators.ptx index 157960858..3557aeffc 100644 --- a/pretext/Selection/PrecedenceofOperators.ptx +++ b/pretext/Selection/PrecedenceofOperators.ptx @@ -117,33 +117,33 @@

((5*3) > 10) and ((4+6) == 11)

- +

Yes, * and + have higher precedence, followed by > and ==, and then the keyword "and" - +

(5*(3 > 10)) and (4 + (6 == 11))

- +

Arithmetic operators (*, +) have higher precedence than comparison operators (>, ==) - +

((((5*3) > 10) and 4)+6) == 11

- +

This grouping assumes Python simply evaluates from left to right, which is incorrect. It follows the precedence listed in the table in this section. - +

((5*3) > (10 and (4+6))) == 11

- +

This grouping assumes that "and" has a higher precedence than ==, which is not true. - +

diff --git a/pretext/SimplePythonData/Input.ptx b/pretext/SimplePythonData/Input.ptx index 004509aef..9978d521d 100644 --- a/pretext/SimplePythonData/Input.ptx +++ b/pretext/SimplePythonData/Input.ptx @@ -66,33 +66,33 @@ print ( type(n) )

<class 'str'>

- +

All input from users is read in as a string. - +

<class 'int'>

- +

Even though the user typed in an integer, it does not come into the program as an integer. - +

<class 18>

- +

18 is the value of what the user typed, not the type of the data. - +

18

- +

18 is the value of what the user typed, not the type of the data. - +

diff --git a/pretext/SimplePythonData/OperatorsandOperands.ptx b/pretext/SimplePythonData/OperatorsandOperands.ptx index 4ae5e4825..7d4923403 100644 --- a/pretext/SimplePythonData/OperatorsandOperands.ptx +++ b/pretext/SimplePythonData/OperatorsandOperands.ptx @@ -113,33 +113,33 @@ print(18 / 4)

4.5

- +

The / operator does exact division and returns a floating point result. - +

5

- +

The / operator does exact division and returns a floating point result. - +

4

- +

The / operator does exact division and returns a floating point result. - +

2

- +

The / operator does exact division and returns a floating point result. - +

@@ -157,33 +157,33 @@ print(18 // 4)

4.25

- +

- The // operator does integer division and returns an integer result - +

5

- +

- The // operator does integer division and returns an integer result, but it truncates the result of the division. It does not round. - +

4

- +

- The // operator does integer division and returns the truncated integer result. - +

2

- +

- The // operator does integer division and returns the result of the division on an integer (not the remainder). - +

@@ -201,33 +201,33 @@ print(18 % 4)

4.25

- +

The % operator returns the remainder after division. - +

5

- +

The % operator returns the remainder after division. - +

4

- +

The % operator returns the remainder after division. - +

2

- +

The % operator returns the remainder after division. - +

diff --git a/pretext/SimplePythonData/OrderofOperations.ptx b/pretext/SimplePythonData/OrderofOperations.ptx index 7b5bfaf3e..27703303f 100644 --- a/pretext/SimplePythonData/OrderofOperations.ptx +++ b/pretext/SimplePythonData/OrderofOperations.ptx @@ -73,33 +73,33 @@ print((2 ** 3) ** 2) # use parentheses to force the order you want!

14

- +

Using parentheses, the expression is evaluated as (2*5) first, then (10 // 3), then (16-3), and then (13+1). - +

24

- +

Remember that * has precedence over -. - +

3

- +

Remember that // has precedence over -. - +

13.667

- +

Remember that // does integer division. - +

@@ -117,33 +117,33 @@ print((2 ** 3) ** 2) # use parentheses to force the order you want!

768

- +

Exponentiation has precedence over multiplication, but its precedence goes from right to left! So 2 ** 3 is 8, 2 ** 8 is 256 and 256 * 3 is 768. - +

128

- +

Exponentiation (**) is processed right to left, so take 2 ** 3 first. - +

12

- +

There are two exponentiations. - +

256

- +

Remember to multiply by 3. - +

diff --git a/pretext/SimplePythonData/Reassignment.ptx b/pretext/SimplePythonData/Reassignment.ptx index 3d44e3d37..0fb400526 100644 --- a/pretext/SimplePythonData/Reassignment.ptx +++ b/pretext/SimplePythonData/Reassignment.ptx @@ -67,33 +67,33 @@ x = 22

x is 15 and y is 15

- +

Look at the last assignment statement which gives x a different value. - +

x is 22 and y is 22

- +

No, x and y are two separate variables. Just because x changes in the last assignment statement, it does not change the value that was copied into y in the second statement. - +

x is 15 and y is 22

- +

Look at the last assignment statement, which reassigns x, and not y. - +

x is 22 and y is 15

- +

Yes, x has the value 22 and y the value 15. - +

diff --git a/pretext/SimplePythonData/Typeconversionfunctions.ptx b/pretext/SimplePythonData/Typeconversionfunctions.ptx index 098037cb5..4f7fe1369 100644 --- a/pretext/SimplePythonData/Typeconversionfunctions.ptx +++ b/pretext/SimplePythonData/Typeconversionfunctions.ptx @@ -58,33 +58,33 @@ print( int(53.785) )

Nothing is printed. It generates a runtime error.

- +

The statement is valid Python code. It calls the int function on 53.785 and then prints the value that is returned. - +

53

- +

The int function truncates all values after the decimal and prints the integer value. - +

54

- +

When converting to an integer, the int function does not round. - +

53.785

- +

The int function removes the fractional part of 53.785 and returns an integer, which is then printed. - +

diff --git a/pretext/SimplePythonData/UpdatingVariables.ptx b/pretext/SimplePythonData/UpdatingVariables.ptx index 2f1a0d58b..c2d069433 100644 --- a/pretext/SimplePythonData/UpdatingVariables.ptx +++ b/pretext/SimplePythonData/UpdatingVariables.ptx @@ -71,33 +71,33 @@ print(x)

12

- +

The value of x changes in the second statement. - +

-1

- +

In the second statement, substitute the current value of x before subtracting 1. - +

11

- +

Yes, this statement sets the value of x equal to the current value minus 1. - +

Nothing. An error occurs because x can never be equal to x - 1.

- +

Remember that variables in Python are different from variables in math in that they (temporarily) hold values, but can be reassigned. - +

@@ -119,33 +119,33 @@ print(x)

12

- +

The value of x changes in the second statement. - +

9

- +

Each statement changes the value of x, so 9 is not the final result. - +

15

- +

Yes, starting with 12, subtract 3, than add 5, and finally add 1. - +

Nothing. An error occurs because x cannot be used that many times in assignment statements.

- +

Remember that variables in Python are different from variables in math in that they (temporarily) hold values, but can be reassigned. - +

diff --git a/pretext/SimplePythonData/ValuesandDataTypes.ptx b/pretext/SimplePythonData/ValuesandDataTypes.ptx index 7d916bd86..8a44b0e2c 100644 --- a/pretext/SimplePythonData/ValuesandDataTypes.ptx +++ b/pretext/SimplePythonData/ValuesandDataTypes.ptx @@ -136,33 +136,33 @@ print(3.4, "hello", 45)

Print out the value and determine the data type based on the value printed.

- +

You may be able to determine the data type based on the printed value, but it may also be deceptive, like when a string prints, there are no quotes around it. - +

Use the type function.

- +

The type function will tell you the class the value belongs to. - +

Use it in a known equation and print the result.

- +

Only numeric values can be used in equations. - +

Look at the declaration of the variable.

- +

In Python variables are not declared. - +

@@ -175,33 +175,33 @@ print(3.4, "hello", 45)

Character

- +

It is not a single character. - +

Integer

- +

The data is not numeric. - +

Float

- +

The value is not numeric with a decimal point. - +

String

- +

Strings can be enclosed in single quotes. - +

diff --git a/pretext/SimplePythonData/VariableNamesandKeywords.ptx b/pretext/SimplePythonData/VariableNamesandKeywords.ptx index 2a613d649..e941aa3ba 100644 --- a/pretext/SimplePythonData/VariableNamesandKeywords.ptx +++ b/pretext/SimplePythonData/VariableNamesandKeywords.ptx @@ -182,17 +182,17 @@ class = "Computer Science 101"

True

- +

- The + character is not allowed in variable names. - +

False

- +

- The + character is not allowed in variable names (everything else in this name is fine). - +

diff --git a/pretext/SimplePythonData/Variables.ptx b/pretext/SimplePythonData/Variables.ptx index 4d72c6cb0..d1fc578ef 100644 --- a/pretext/SimplePythonData/Variables.ptx +++ b/pretext/SimplePythonData/Variables.ptx @@ -127,33 +127,33 @@ print(day)

Nothing is printed. A runtime error occurs.

- +

It is legal to change the type of data that a variable holds in Python. - +

Thursday

- +

This is the first value assigned to the variable day, but the next statements reassign that variable to new values. - +

32.5

- +

This is the second value assigned to the variable day, but the next statement reassigns that variable to a new value. - +

19

- +

The variable day will contain the last value assigned to it when it is printed. - +

diff --git a/pretext/Strings/IndexOperatorWorkingwiththeCharactersofaString.ptx b/pretext/Strings/IndexOperatorWorkingwiththeCharactersofaString.ptx index 89670b19a..27927c2fe 100644 --- a/pretext/Strings/IndexOperatorWorkingwiththeCharactersofaString.ptx +++ b/pretext/Strings/IndexOperatorWorkingwiththeCharactersofaString.ptx @@ -49,33 +49,33 @@ print(s[3])

t

- +

Index locations do not start with 1, they start with 0. - +

h

- +

Yes, index locations start with 0. - +

c

- +

s[-3] would return c, counting from right to left. - +

Error, you cannot use the [ ] operator with a string.

- +

[ ] is the index operator - +

@@ -94,33 +94,33 @@ print(s[2] + s[-5])

tr

- +

Yes, indexing operator has precedence over concatenation. - +

ps

- +

p is at location 0, not 2. - +

nn

- +

n is at location 5, not 2. - +

Error, you cannot use the [ ] operator with the + operator.

- +

[ ] operator returns a string that can be concatenated with another string. - +

diff --git a/pretext/Strings/Length.ptx b/pretext/Strings/Length.ptx index 606ba8d6a..7e026997c 100644 --- a/pretext/Strings/Length.ptx +++ b/pretext/Strings/Length.ptx @@ -54,17 +54,17 @@ print(len(s))

11

- +

The blank counts as a character. - +

12

- +

Yes, there are 12 characters in the string. - +

@@ -83,33 +83,33 @@ print(s[len(s)-5])

o

- +

Take a look at the index calculation again, len(s)-5. - +

r

- +

Yes, len(s) is 12 and 12-5 is 7. Use 7 as index and remember to start counting with 0. - +

s

- +

s is at index 11 - +

Error, len(s) is 12 and there is no index 12.

- +

You subtract 5 before using the index operator so it will work. - +

@@ -128,33 +128,33 @@ print(s[-3])

c

- +

Yes, 3 characters from the end. - +

k

- +

Count backward 3 characters. - +

s

- +

When expressed with a negative index the last character s is at index -1. - +

Error, negative indices are illegal.

- +

Python does use negative indices to count backward from the end. - +

diff --git a/pretext/Strings/OperationsonStrings.ptx b/pretext/Strings/OperationsonStrings.ptx index 274b1d611..4bb0d6bb8 100644 --- a/pretext/Strings/OperationsonStrings.ptx +++ b/pretext/Strings/OperationsonStrings.ptx @@ -65,33 +65,33 @@ print(s + t)

python rocks

- +

Concatenation does not automatically add a space. - +

python

- +

The expression s+t is evaluated first, then the resulting string is printed. - +

pythonrocks

- +

Yes, the two strings are glued end to end. - +

Error, you cannot add two strings together.

- +

The + operator has different meanings depending on the operands, in this case, two strings. - +

@@ -111,33 +111,33 @@ print(s+excl*3)

python!!!

- +

Yes, repetition has precedence over concatenation - +

python!python!python!

- +

Repetition is done first. - +

pythonpythonpython!

- +

The repetition operator is working on the excl variable. - +

Error, you cannot perform concatenation and repetition at the same time.

- +

The + and * operator are defined for strings as well as numbers. - +

diff --git a/pretext/Strings/StringComparison.ptx b/pretext/Strings/StringComparison.ptx index 18e900be0..139efba61 100644 --- a/pretext/Strings/StringComparison.ptx +++ b/pretext/Strings/StringComparison.ptx @@ -86,17 +86,17 @@ print(ord(" "))

True

- +

Both match up to the g but Dog is shorter than Doghouse so it comes first in the dictionary. - +

False

- +

Strings are compared character by character. - +

@@ -114,25 +114,25 @@ print(ord(" "))

True

- +

d is greater than D according to the ord function (68 versus 100). - +

False

- +

Yes, upper case is less than lower case according to the ordinal values of the characters. - +

They are the same word

- +

Python is case sensitive meaning that upper case and lower case characters are different. - +

@@ -150,17 +150,17 @@ print(ord(" "))

True

- +

d is greater than D. - +

False

- +

The length does not matter. Lower case d is greater than upper case D. - +

diff --git a/pretext/Strings/StringMethods.ptx b/pretext/Strings/StringMethods.ptx index 6fa2be71d..3dfa78f8b 100644 --- a/pretext/Strings/StringMethods.ptx +++ b/pretext/Strings/StringMethods.ptx @@ -269,25 +269,25 @@ print(s.count("o") + s.count("p"))

0

- +

There are definitely o and p characters. - +

2

- +

There are 2 o characters but what about p? - +

3

- +

Yes, add the number of o characters and the number of p characters. - +

@@ -306,33 +306,33 @@ print(s[1] * s.index("n"))

yyyyy

- +

Yes, s[1] is y and the index of n is 5, so 5 y characters. It is important to realize that the index method has precedence over the repetition operator. Repetition is done last. - +

55555

- +

Close. 5 is not repeated, it is the number of times to repeat. - +

n

- +

This expression uses the index of n - +

Error, you cannot combine all those things together.

- +

This is fine, the repetition operator used the result of indexing and the index method. - +

@@ -454,33 +454,33 @@ print('sum of {} and {} is {}; product: {}.'.format( x, y, x+y, x*y))

Nothing - it causes an error

- +

It is legal format syntax: put the data in place of the braces. - +

sum of {} and {} is {}; product: {}. 2 6 8 12

- +

Put the data into the format string; not after it. - +

sum of 2 and 6 is 8; product: 12.

- +

Yes, correct substitutions! - +

sum of {2} and {6} is {8}; product: {12}.

- +

Close: REPLACE the braces. - +

@@ -499,25 +499,25 @@ print('{:.1f} {:.2f} {:.7f}'.format(v, v, v))

2.34567 2.34567 2.34567

- +

The numbers before the f in the braces give the number of digits to display after the decimal point. - +

2.3 2.34 2.34567

- +

Close, but round to the number of digits and display the full number of digits specified. - +

2.3 2.35 2.3456700

- +

Yes, correct number of digits with rounding! - +

diff --git a/pretext/Strings/StringsareImmutable.ptx b/pretext/Strings/StringsareImmutable.ptx index dbf30ade9..08525be5c 100644 --- a/pretext/Strings/StringsareImmutable.ptx +++ b/pretext/Strings/StringsareImmutable.ptx @@ -44,25 +44,25 @@ print(s)

Ball

- +

Assignment is not allowed with strings. - +

Call

- +

Assignment is not allowed with strings. - +

Error

- +

Yes, strings are immutable. - +

diff --git a/pretext/Strings/TheAccumulatorPatternwithStrings.ptx b/pretext/Strings/TheAccumulatorPatternwithStrings.ptx index 9ef9bc2ee..d32bdfb7c 100644 --- a/pretext/Strings/TheAccumulatorPatternwithStrings.ptx +++ b/pretext/Strings/TheAccumulatorPatternwithStrings.ptx @@ -70,25 +70,25 @@ print(r)

Ball

- +

Each item is converted to upper case before concatenation. - +

BALL

- +

Each character is converted to upper case but the order is wrong. - +

LLAB

- +

Yes, the order is reversed due to the order of the concatenation. - +

diff --git a/pretext/Strings/TheSliceOperator.ptx b/pretext/Strings/TheSliceOperator.ptx index 9a038c491..ae991ef08 100644 --- a/pretext/Strings/TheSliceOperator.ptx +++ b/pretext/Strings/TheSliceOperator.ptx @@ -50,33 +50,33 @@ print(s[3:8])

python

- +

That would be s[0:6]. - +

rocks

- +

That would be s[7:]. - +

hon r

- +

Yes, start with the character at index 3 and go up to but not include the character at index 8. - +

Error, you cannot have two numbers inside the [ ].

- +

This is called slicing, not indexing. It requires a start and an end. - +

@@ -95,33 +95,33 @@ print(s[7:11] * 3)

rockrockrock

- +

Yes, rock starts at 7 and goes through 10. Repeat it 3 times. - +

rock rock rock

- +

Repetition does not add a space. - +

rocksrocksrocks

- +

Slicing will not include the character at index 11. Just up to it (10 in this case). - +

Error, you cannot use repetition with slicing.

- +

The slice will happen first, then the repetition. So it is ok. - +

diff --git a/pretext/Strings/TraversalandtheforLoopByIndex.ptx b/pretext/Strings/TraversalandtheforLoopByIndex.ptx index baaa97702..48a798776 100644 --- a/pretext/Strings/TraversalandtheforLoopByIndex.ptx +++ b/pretext/Strings/TraversalandtheforLoopByIndex.ptx @@ -55,33 +55,33 @@ for idx in range(len(s)):

0

- +

The for loop visits each index but the selection only prints some of them. - +

1

- +

o is at positions 4 and 8 - +

2

- +

Yes, it will print all the characters in even index positions and the o character appears both times in an even location. - +

Error, the for statement cannot have an if inside.

- +

The for statement can have any statements inside, including if as well as for. - +

diff --git a/pretext/Strings/TraversalandtheforLoopByItem.ptx b/pretext/Strings/TraversalandtheforLoopByItem.ptx index 55ae76d80..4cf2e5254 100644 --- a/pretext/Strings/TraversalandtheforLoopByItem.ptx +++ b/pretext/Strings/TraversalandtheforLoopByItem.ptx @@ -50,33 +50,33 @@ for ch in s:

10

- +

Iteration by item will process once for each item in the sequence. - +

11

- +

The blank is part of the sequence. - +

12

- +

Yes, there are 12 characters, including the blank. - +

Error, the for statement needs to use the range function.

- +

The for statement can iterate over a sequence item by item. - +

@@ -96,33 +96,33 @@ for ch in s[3:8]:

4

- +

Slice returns a sequence that can be iterated over. - +

5

- +

Yes, The blank is part of the sequence returned by slice - +

6

- +

Check the result of s[3:8]. It does not include the item at index 8. - +

Error, the for statement cannot use slice.

- +

Slice returns a sequence. - +

diff --git a/pretext/Strings/TraversalandthewhileLoop.ptx b/pretext/Strings/TraversalandthewhileLoop.ptx index c76db6f79..691d4ebc4 100644 --- a/pretext/Strings/TraversalandthewhileLoop.ptx +++ b/pretext/Strings/TraversalandthewhileLoop.ptx @@ -51,25 +51,25 @@ while idx < len(s):

0

- +

Yes, idx goes through the odd numbers starting at 1. o is at position 4 and 8. - +

1

- +

o is at positions 4 and 8. idx starts at 1, not 0. - +

2

- +

There are 2 o characters but idx does not take on the correct index values. - +

diff --git a/pretext/UnitTesting/CheckingAssumptionsWithAssert.ptx b/pretext/UnitTesting/CheckingAssumptionsWithAssert.ptx index 55c98b7c4..4ffab6048 100644 --- a/pretext/UnitTesting/CheckingAssumptionsWithAssert.ptx +++ b/pretext/UnitTesting/CheckingAssumptionsWithAssert.ptx @@ -199,17 +199,17 @@ print(sumnums(3, 1))

True

- +

Incorrect. Output is displayed only if the condition is False. - +

False

- +

Correct. Output is displayed only if the condition is False. - +

@@ -234,33 +234,33 @@ def getfirst(msg):

assert len(msg) <= 0

- +

Incorrect. Use the precondition as the condition for the assert. - +

assert len(msg) > 0

- +

Correct. Use the precondition as the condition for the assert. - +

assert msg[0]

- +

Incorrect. Use the precondition as the condition for the assert. - +

none of these

- +

Incorrect. Use the precondition as the condition for the assert. - +

diff --git a/pretext/UnitTesting/DesigningTestableFunctions.ptx b/pretext/UnitTesting/DesigningTestableFunctions.ptx index 75047466d..5a22bb51f 100644 --- a/pretext/UnitTesting/DesigningTestableFunctions.ptx +++ b/pretext/UnitTesting/DesigningTestableFunctions.ptx @@ -45,17 +45,17 @@ def add(x, y):

Yes.

- +

Correct. In a unit test, an assert statement could check the value of sum after a call to add. - +

No.

- +

Incorrect. The function stores its result in a global variable, which can be checked by an assert statement. - +

@@ -152,33 +152,33 @@ def getfirst(msg):

len(msg) <= 0

- +

Incorrect. The function would crash if this condition were true. - +

len(msg) > 0

- +

Correct. The function cannot return the first character of an empty msg. - +

msg == ""

- +

Incorrect. The function would crash if this condition were true. - +

none of these

- +

Incorrect. Try again. - +

diff --git a/pretext/UnitTesting/TestFirstDevelopment.ptx b/pretext/UnitTesting/TestFirstDevelopment.ptx index ae03f2141..b7b50deb3 100644 --- a/pretext/UnitTesting/TestFirstDevelopment.ptx +++ b/pretext/UnitTesting/TestFirstDevelopment.ptx @@ -146,17 +146,17 @@ print("All tests passed!")

True

- +

Correct. Test-First Development requires the developer to write unit tests. However, these tests come with significant benefits and can speed up overall development. - +

False

- +

Incorrect. Test-First Development requires the developer to write unit tests. However, these tests come with significant benefits and can speed up overall development. - +

diff --git a/pretext/UnitTesting/TestingFunctions.ptx b/pretext/UnitTesting/TestingFunctions.ptx index c2ac5dd49..8124032da 100644 --- a/pretext/UnitTesting/TestingFunctions.ptx +++ b/pretext/UnitTesting/TestingFunctions.ptx @@ -177,33 +177,33 @@ if result != 'OK':

assert result != 'OK'

- +

Incorrect. The assert should call the engage_thruster function. Try again. - +

assert engage_thruster(22) == result

- +

Incorrect. The assert should not assume the presence of a variable named result. - +

assert engage_thruster(22) != 'OK'

- +

Incorrect. This assert calls the engage_thruster function with the correct parameter, but interprets the result differently from the if statement provided. - +

assert engage_thruster(22) == 'OK'

- +

Correct! This assert checks that the result of invoking engage_thruster(22) is the value 'OK'. - +

@@ -221,33 +221,33 @@ if result != 'OK':

Unit test

- +

Incorrect. The assertion correctly checks that get_first should return the first character in 'Bells', 'B'. - +

Tested function

- +

Correct. The function code returns the second character of the parameter, not the first. - +

Both are in error

- +

Incorrect. The assertion correctly checks that get_first should return the first character in 'Bells', 'B'. - +

Both are correct

- +

Incorrect. The function code returns the second character of the parameter, not the first. - +

diff --git a/pretext/UnitTesting/WritingUnitTests.ptx b/pretext/UnitTesting/WritingUnitTests.ptx index a1188abd6..712aac1db 100644 --- a/pretext/UnitTesting/WritingUnitTests.ptx +++ b/pretext/UnitTesting/WritingUnitTests.ptx @@ -89,33 +89,33 @@ assert sumnums(1, 1) == 1

assert repeat('*', 0) == ''

- +

Correct. The specification indicates that the function should produce an empty string. - +

assert repeat('*', -1) == ''

- +

Incorrect. The parameter -1 violates the precondition, and such a test is inappropriate, even though the function would return an empty string in this case. - +

assert repeat('-', 5) == '-----'

- +

Correct. The parameters satisfy the precondition and the value '-----' is appropriate given the postcondition. - +

assert repeat('*', 5) == '***'

- +

Incorrect. The string '***' does not conform to the postcondition. - +

diff --git a/pretext/navhelp.ptx b/pretext/navhelp.ptx index a35e961ea..6757338cf 100644 --- a/pretext/navhelp.ptx +++ b/pretext/navhelp.ptx @@ -57,33 +57,33 @@ print(2 + 3)

Python

- +

Yes, Python is a great language to learn, whether you are a beginner or an experienced programmer. - +

Java

- +

Java is a good object oriented language but it has some details that make it hard for the beginner. - +

C

- +

C is an imperative programming language that has been around for a long time, but it is not the one that we use. - +

ML

- +

No, ML is a functional programming language. You can use Python to write functional programs as well. - +

@@ -98,33 +98,33 @@ print(2 + 3)

red

- +

Red is a definitely on of the colors. - +

yellow

- +

Yes, yellow is correct. - +

black

- +

Remember the acronym...ROY G BIV. B stands for blue. - +

green

- +

Yes, green is one of the colors. - +

diff --git a/requirements.txt b/requirements.txt index 20a98cf59..ec5dd8e48 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1 +1 @@ -pretextbook >= 0.8.0 +pretext == 1.6.0