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. -
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 -
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! -
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. -
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. -
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. -
True
Yes, dog is a key in the dictionary. -
False
The in operator returns True if a key is in the dictionary, False otherwise. -
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. -
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. -
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. -
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. -
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. -
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. -
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. -
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. -
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. -
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). -
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. -
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__'. -
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". -
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. -
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. -
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). -
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. -
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 (:). -
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. -
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. -
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. -
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. -
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. -
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. -
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. -
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. -
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. -
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. -
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. -
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. -
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. -
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. -
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. -
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. -
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. -
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. -
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. -
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. -
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. -
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. -
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. -
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? -
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 -
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. -
[ ]
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. -
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. -
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. -
[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. -
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 -
[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. -
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. -
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. -
[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. -
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. -
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. -
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. -
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. -
[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. -
[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. -
[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. -
[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. -
[ [ ], 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. -
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. -
[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. -
[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. -
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. -
[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. -
[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. -
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. -
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. -
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. -
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. -
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). -
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. -
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. -
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. -
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. -
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. -
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. -
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. -
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 -
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. -
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. -
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. -
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. -
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. -
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. -
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. -
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. -
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. -
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. -
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. -
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. -
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. -
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. -
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. -
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. -
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. -
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. -
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. -
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? -
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()" -
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. -
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. -
- This code would turn the turtle to the south before drawing -
This code would turn the turtle to the west before drawing -
This code would turn the turtle to the south before drawing -
This code is almost correct, but the short end would be facing east instead of west. -
Yes, the turtle starts facing east, so to turn it north you can turn left 90 or right 270 degrees. -
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. -
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. -
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. -
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. -
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. -
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. -
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. -
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. -
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. -
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. -
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. -
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. -
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. -
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. -
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. -
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. -
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). -
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. -
((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. -
<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. -
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. -
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). -
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. -
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. -
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. -
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. -
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. -
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. -
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. -
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. -
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. -
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). -
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. -
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 -
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. -
11
The blank counts as a character. -
12
Yes, there are 12 characters in the string. -
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. -
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. -
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. -
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. -
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. -
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. -
True
d is greater than D. -
False
The length does not matter. Lower case d is greater than upper case D. -
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. -
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. -
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. -
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! -
Ball
Assignment is not allowed with strings. -
Call
Assignment is not allowed with strings. -
Error
Yes, strings are immutable. -
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. -
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. -
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. -
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. -
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. -
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. -
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. -
True
Incorrect. Output is displayed only if the condition is False. -
False
Correct. Output is displayed only if the condition is False. -
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. -
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. -
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. -
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. -
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'. -
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. -
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. -
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. -
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. -