Question: Python 3 confirmed. How many cookies do you want to make? 30 Traceback (most recent call last): File hw01.py, Iine 10, in smodules recipe_mult =12/


Python 3 confirmed. How many cookies do you want to make? 30 Traceback (most recent call last): File "hw01.py", Iine 10, in smodules recipe_mult =12/ num_cookies TypeError: unsupported operand type(s) for I: "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 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 I 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 import platform vers = platform.python_version() assert vers[0] == ' 3 ', "You must use Python 3 , "+verst" is not acceptal print("Python 3 confirmed.") \# 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(recipemult)) + "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)
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
