First, uncomment the following block of code, by removing all of the \# symbols before each line. You can do this quickly if you're using VS Code by selecting the entire block of code, going into the Edit menu, and selecting "Toggle Line Comment". Leave the code at the bottom that has to do with loan payment calculation commented out for now. num_cookies = input("How many cookies do you want to make? ") recipe_mult = 12/ num_cookies butter = str(125*recipe_mult)+"g butter" sugar = 225 recipe_mult+"g sugar" eggs = str(max(1, round(recipe_mult))+" eggs" vanilla = str(recipe_mult)+" tsp vanilla extract" flour = str(225*recipe_mult)+g flour salt = str(0.5*recipe_mult)+" tsp salt" str(200*recipe_mult)+"g chocolate chips" = chips print(butter) print("sugar") print(eggs) print(vanilla) print(flour) print(salt) print(chips) The code is intended to take a recipe that normally makes 12 cookies ( 125g butter, 225g sugar, 1egg,1 tsp vanilla extract, 225g flour, 0.5 tsp salt, 200g chocolate chips), and adjust it to work with any number of cookies the user inputs. This is mostly done by computing a factor recipe_mult to multiply the quantity of each ingredient by, applying that factor, and then using string concatenation (+ with two strings) to attach that number to the units and the name of the ingredient. The The code is intended to take a recipe that normally makes 12 cookies (125g butter, 225g sugar, 1 egg, 1 tsp vanilla extract, 225g flour, 0.5tsp salt, 200g chocolate chips), and adjust it to work with any number of cookies the user inputs. This is mostly done by computing a factor recipe_mult to multiply the quantity of each ingredient by, applying that factor, and then using string concatenation (+ with two strings) to attach that number to the units and the name of the ingredient. The exception to this is eggs, since it's difficult to add parts of an egg to the recipe, we just round to the nearest whole number of eggs, with a minimum of 1 . Unfortunately, this program does not work because it contains many errors, and is a good example of why you should never try to write an entire program at once: write a few lines of code, test those lines, and then move on only if it works. Try running the code as before: save the file in VS Code, then run it from the terminal/command prompt using python3 hw01.py or py hw01.py. This time you should immediately get our first syntax error: if you're using terminal this is indicated by a caret symbol pointing at the location in the code where Python believes that a syntax error occurred, which should be the variable name vanilla in this expression on line 14 : File "hwe1.py", line 14 vanilla = str(recipe_mult)+" tsp vanilla extract" ^ SyntaxError: invalid syntax This is perhaps the single most annoying type of error to debug in Python, because the pointer feature is wildly unhelpful here: the error actually occurs on the previous line (line 13). The error in question here is a missing parenthesis to close out the str function: you'll notice that the line contains three left narens but only two right This is perhaps the single most annoying type of error to debug in Python, because the pointer feature is wildly unhelpful here: the error actually occurs on the previous line (line 13). The error in question here is a missing parenthesis to close out the str function: you'll notice that the line contains three left parens but only two right parens. You are pretty much guaranteed to make this mistake yourself at some point in the future, so keep this in mind: if you get a syntax error at the start of a given line and have no idea why, it's probably due to a missing parenthesis on the line before that. Add the matching parens to the line in question here: \[ \begin{array}{l} \text { eggs }=\operatorname{str}(\max (1, \text { round }(\text { recipe_mult })))+" \\ \text { eggs" } \end{array} \] Now try saving and then running the code again, and you'll get another syntax error pop-up, this time causing "flour" at the end of line 15 to be pointed to: \[ \begin{array}{l} \text { File "hw01.py", line } 15 \\ \quad \text { flour }=\operatorname{str}\left(225^{*} \text { recipe_mult } ight)+\mathrm{g} \text { flour } \end{array} \] SyntaxError: invalid syntax This time the problem is that we're missing the quotes around g flour, so rather than treating " g flour" as a string, to concatenate to the end of whatever number we get by multiplying 225 by our recipe_mult factor, Python assumes that g and flour are both variables. However, we have a space between the two variables without any valid operation to connect them, like + or * . This isn't allowed in Python no matter what the values of those variables are, so we get a syntax error. Fix this by adding quotes around " g flour", like this: \[ \text { flour }=\operatorname{str}\left(225^{*} \text { recipe_mult } ight)+\text { "g flour" } \] flour =str(225 recipe_mult )+g flour" Running the code again will yield yet another syntax error, though this time Python will at least give a bit more information on what the problem is: you should get the error message "can't assign to operator" on line 17 : File "hwe1.py", line 17 str(200*recipe_mult)+"g chocolate chips" = chips SyntaxError: cannot assign to operator The problem here is that you can't use the assignment operator if the thing on the left side is anything other than a variable, because the assignment operator is not the same as mathematical equals. The assignment operator takes whatever expression is on the right side of the =, evaluates it, and stores that value in whatever variable is specified on the left. So to fix the line, we just put the variable (chips) on the left side, and the expression we're trying to evaluate and assign to chips (str(200*recipe_mult)+"g chocolate chips") on the right: chips = str (20 recipe_mult )+ "g chocolate chips" If we run the program again, then you'll notice that we don't get any syntax errors, and the code starts running. This means that we have fixed all syntax errors, so only runtime and logic errors remain. Put in a reasonable number for the number of cookies when prompted (say 30 , so that we should end up with 2.5 times the ingredients as the base recipe), and you should encounter our first runtime error, which will yield a message like this in the terminal window: Python 3 confirmed. How many cookies do vou want to make? 30 Python 3 confirmed. How many cookies do you want to make? 30 Traceback (most recent call last): File "hw1.py", line 10, in module> recipe_mult =12/ num_cookies TypeError: unsupported operand type(s) for 1: 'int' and 'str' This is a runtime error: unlike with a syntax error, the program started running, and got through the first 9 lines before crashing on line 10. You can tell what line the program crashed on because Python prints out both the line number and the line itself as part of the error message, as 1 have highlighted above. The error given is "TypeError: unsupported operand type(s) for /: 'int' and 'str"', which means that at some point in the line in question, we are trying to use division (/) on something of type int (integer) and something of type str (string). Since division is only used once in that line, we can conclude that the integer in question is 12 , and the string is num_cookies. We can confirm this by adding an additional line just before the line where the error occurs that prints out the type of num_cookies: print("type of num_cookies is", type(num_cookies)) This should print out "type of num_cookies is " when the program is run just before the error happens, which confirms that num_cookies is a string. even though we want it to be an integer. Adding additional print statements to check on the type or value of a variable midway through a program, especially just before an error occurs, is one of the best ways to determine what's causing a given runtime or logic error. You should delete this line or comment it out by adding a \# sign in front of it after you have confirmed this information, since it's not part of the intended operation of the program, and is only used for debugging. However, even though we know what the problem is now (we can't use num_cookies for division if it's a string), we still need to figure out the root cause (why is num_cookies a string in the first place?). Unlike syntax errors, runtime errors often require going back to a previous line to determine why the type or value of a given variable is incorrect: in this case, we need to determine why num_cookies, which is a variable that represents the number of cookies the user wants, is of type string, rather than of type integer. To do this, we look at the line where num_cookies is defined: num_cookies = input ("How many cookies do you want to make? ") The problem here is that the input function always returns a value of type string, even when you type in a number, so when we typed in 30 , num_cookies was set to the string " 30 " rather than the number 30 . To fix this, we need to convert the value that was input from a string into an integer, using the int function like so: num_cookies = int(input("How many cookies do you want to make? ")) Run the program again, and you'll encounter a different runtime error, this time on line 13 : Traceback (most recent call last): File "hw1.py", line 13, in smodules sugar =225 recipe_mult+"g sugar" TypeError: unsupported operand type(s) for + : 'float' and 'str' Again, we can deduce what the problem is by looking at the error: it says using the + operation on objects of type "float" and "str" is unsupported: since there's only one use of + in the line in anestion we can conclude that the Again, we can deduce what the problem is by looking at the error: it says using the + operation on objects of type "float" and "str" is unsupported: since there's only one use of + in the line in question, we can conclude that the thing on the left of that operation ( 225 recipe_mult) evaluates to a floating point number, but the thing on the right ("g sugar") is a string. This error occurs because you can only use + on two numbers or two strings, not one of each. In this case, we're trying to use string concatenation, where we take the number yielded from (225*recipe_mult) and put the letters "g sugar" on the end, generating a string like "562.5g sugar", so we want both operands to be strings. To do this, we need to convert the expression on the left to a string using the str function before we can add it to the string on the right: \[ \text { sugar }=\operatorname{str}\left(225^{*} \text { recipe_mult } ight)+\text { "g sugar" } \] If we run the program after fixing this error, then the program will actually run to completion, meaning that we have fixed all of the runtime errors (at least all of the ones that we would encounter for the given input: more on this later in the course when we discuss control flow). However, you'll notice that the output doesn't make sense for the number of cookies input: for example, if we type in 30 cookies, we get this: Python 3 confirmed. How many cookies do you want to make? 30 50.0g butter sugar 1 eggs 0.4 tsp vanilla extract 90.0g flour 0.2 tsp salt 80.0g chocolate chips There are two isstres ficte-: inst, unefougtam just prints 9:42 There are two issues here: first, the program just prints out "sugar" rather than telling us how many grams of sugar we need, and second, the amounts for each ingredient is not 2.5 times the original recipe as we'd expect: in fact we have lower values for 30 cookies than we did for 12 . These are logic errors: coding mistakes that don't cause the program to crash, but produce an output that is not what the programmer intended. The first problem can be fixed by just looking at the code: it prints sugar because we are telling it to print the string "sugar" rather than the variable sugar: print("sugar") To fix this, just get rid of the quotes around sugar so it looks for a variable named sugar and prints out the contents of that variable, rather than literally printing "sugar". The other issue is a bit harder to handle, since it affects all of the numerical values being printed. To fix logical errors like this, we typically print out the value of various variables at points throughout the program, to determine what's going wrong. But to do this, we first need to determine what the value of a given variable should be at that point in the code. We'll start by inspecting the value of num_cookies and recipe_mult, since all the other variables depend on them. If we use 30 cookies as our test again, then num_cookies should be 30 , and recipe_mult should be 2.5. So just after the line where recipe_mult is first defined, we'll print the value of both of them, along with a string that allows us to tell which one is which: print("num_cookies is", num_cookies) print("recipe_mult is", recipe_mult) Running the program should then resultin the following However, even though we know what the problem is now (we can't use num_cookies for division if it's a string), we still need to figure out the root cause (why is num_cookies a string in the first place?). Unlike syntax errors, runtime errors often require going back to a previous line to determine why the type or value of a given variable is incorrect: in this case, we need to determine why num_cookies, which is a variable that represents the number of cookies the user wants, is of type string, rather than of type integer. To do this, we look at the line where num_cookies is defined: num_cookies = input ("How many cookies do you want to make? ") The problem here is that the input function always returns a value of type string, even when you type in a number, so when we typed in 30 , num_cookies was set to the string " 30 " rather than the number 30 . To fix this, we need to convert the value that was input from a string into an integer, using the int function like so: num_cookies = int(input("How many cookies do you want to make? ")) Run the program again, and you'll encounter a different runtime error, this time on line 13 : Traceback (most recent call last): File "hw1.py", line 13, in smodules sugar =225 recipe_mult+"g sugar" TypeError: unsupported operand type(s) for + : 'float' and 'str' Again, we can deduce what the problem is by looking at the error: it says using the + operation on objects of type "float" and "str" is unsupported: since there's only one use of + in the line in anestion we can conclude that the Again, we can deduce what the problem is by looking at the error: it says using the + operation on objects of type "float" and "str" is unsupported: since there's only one use of + in the line in question, we can conclude that the thing on the left of that operation ( 225 recipe_mult) evaluates to a floating point number, but the thing on the right ("g sugar") is a string. This error occurs because you can only use + on two numbers or two strings, not one of each. In this case, we're trying to use string concatenation, where we take the number yielded from (225*recipe_mult) and put the letters "g sugar" on the end, generating a string like "562.5g sugar", so we want both operands to be strings. To do this, we need to convert the expression on the left to a string using the str function before we can add it to the string on the right: \[ \text { sugar }=\operatorname{str}\left(225^{*} \text { recipe_mult } ight)+\text { "g sugar" } \] If we run the program after fixing this error, then the program will actually run to completion, meaning that we have fixed all of the runtime errors (at least all of the ones that we would encounter for the given input: more on this later in the course when we discuss control flow). However, you'll notice that the output doesn't make sense for the number of cookies input: for example, if we type in 30 cookies, we get this: Python 3 confirmed. How many cookies do you want to make? 30 50.0g butter sugar 1 eggs 0.4 tsp vanilla extract 90.0g flour 0.2 tsp salt 80.0g chocolate chips There are two isstres ficte-: inst, unefougtam just prints 9:42 There are two issues here: first, the program just prints out "sugar" rather than telling us how many grams of sugar we need, and second, the amounts for each ingredient is not 2.5 times the original recipe as we'd expect: in fact we have lower values for 30 cookies than we did for 12 . These are logic errors: coding mistakes that don't cause the program to crash, but produce an output that is not what the programmer intended. The first problem can be fixed by just looking at the code: it prints sugar because we are telling it to print the string "sugar" rather than the variable sugar: print("sugar") To fix this, just get rid of the quotes around sugar so it looks for a variable named sugar and prints out the contents of that variable, rather than literally printing "sugar". The other issue is a bit harder to handle, since it affects all of the numerical values being printed. To fix logical errors like this, we typically print out the value of various variables at points throughout the program, to determine what's going wrong. But to do this, we first need to determine what the value of a given variable should be at that point in the code. We'll start by inspecting the value of num_cookies and recipe_mult, since all the other variables depend on them. If we use 30 cookies as our test again, then num_cookies should be 30 , and recipe_mult should be 2.5. So just after the line where recipe_mult is first defined, we'll print the value of both of them, along with a string that allows us to tell which one is which: print("num_cookies is", num_cookies) print("recipe_mult is", recipe_mult) Running the program should then resultin the following print("num_cookies is", num_cookies) print("recipe_mult is", recipe_mult) Running the program should then result in the following output: Python 3 confirmed. How many cookies do you want to make? 30 num_cookies is 30 recipe_mult is 0.4 50.0g butter 90.0g sugar 1 eggs 0.4 tsp vanilla extract 90.0g flour 0.2 tsp salt 80. g chocolate chips You'll notice that although we get the correct value for num_cookies ( 30 ), our value for recipe_mult is incorrect (it should be 2.5, not 0.4 ). So we'll look at the line where recipe_mult is defined to see if we can understand how we got 0.4 : recipe_mult =12/ num_cookies If num_cookies is 30 , then this formula does indeed give us a value of 12/30=0.4 for recipe_mult, so our formula for determining the value to multiply each ingredient by is incorrect. In particular, we have reversed the order of division: to get the number of times larger this recipe should be, we must divide the number of cookies by 12 , not the other way around: recipe_mult = num_cookies /12 After making this fix, running the program should result in the correct values: After making this fix, running the program should result in the correct values: Python 3 confirmed. How many cookies do you want to make? 30 num_cookies is 30 recipe_mult is 2.5 312.5g butter 562.5g sugar 2 eggs 2.5 tsp vanilla extract 562.5g flour 1.25 tsp salt 500.0g chocolate chips Be sure to comment out or delete the lines that print out the values of num_cookies and recipe_mult once you are satisfied that the program is working, since printing those out is not part of the program. You should also try running the program with other values for the number of cookies, to ensure that the program always works for any positive integer input. Your Turn Now, uncomment the second part of the program: amt = float(input("Enter the loan amount in dollars: ") N = int(input("Enter the loan duration in months: ")) r = input("Enter the % annual interest rate: ")/100/12 payment =r amt /1(1+r)(n) print("Your monthly payment is: " + round(payment, 2)) This section of the program is intended to compute the import platform vers = platform.python_version() assert vers []==3 ', "You must use Python 3 , "+vers+" is not acceptable" print("Python 3 confirmed.") \# num_cookies = input ("How many cookies do you want to make? ") \# recipe_mult =12 um_cookies \# butter =str(125 recipe_mult )+ "g butter" \# sugar = 225*recipe_mult+"g sugar" \# eggs =str(max(1, round(recipe_mult ))+" eggs" \# vanilla =str( recipe_mult) +" tsp vanilla extract" \# flour = str(225*recipe_mult )+g flour \# salt = str(0.5*recipe_mult)+" tsp salt" \# str(200*recipe_mult)+"g chocolate chips" = chips \# print (butter) \# print("sugar") \# print(eggs) \# print (vanilla) \# print(flour) \# print(salt) \# print(chips) \# amt = float (input( "Enter the loan amount in dollars: ") \# N= int(input("Enter the loan duration in months: ")) \# r= input ("Enter the \% annual interest rate: ")/100/12 \# payment =r amt /1(1+r)(n) \# print("Your monthly payment is: " + round(payment, 2))