11. The purpose of the following code is to iterate through a list and print a warning message if it finds "user3" in the list. Run this code, analyze its output, and debug it. (If you want to undo your changes to the code, you can click the Reset button.)

How can you fix the error?

  • Change “user3” to “user2” in the conditional.
  • Change the indentation so that the line that prints the warning is not indented.
  • Change “user3” to “user1” in the conditional.
  • Change the != operator to the == operator in the conditional.

12. You did not assign a value to a variable before using it in a conditional. What type of error is this?

  • Index out of bounds
  • Logic error
  • Syntax error
  • Exception

13. Why might you use print statements when debugging code?

  • To prevent errors from occurring
  • To identify which sections of the code are working properly
  • To create error messages
  • To add missing syntax to the code

14. Which of these functions or arguments should you include in a with statement if you want Python to open a file called access.txt so that it can be read? Select three answers.

  • “r”
  • read()
  • open()
  • “access.txt”

15. The logins variable is a string containing 20 device IDs. The device IDs are separated by spaces. In order to pass it into a function that checks the login count of each device, the string should be divided into a list of separate IDs. How do you convert this string into a list and store it in a device_ids variable?

  • device_ids = logins.split()
  • logins.split() as device_ids
  • device_ids = device_ids.split(logins)
  • device_ids = split(device_ids, logins)

16. Fill in the blank: If you use the .split() method to convert a string into a list so that it can be read more easily, this would be an example of _____.

  • slicing
  • parsing
  • debugging
  • dividing

17. After you’ve opened a log file as file, which line of code will help you read the file into a variable called text?

  • text.read(file)
  • text = file.read()
  • text = read(file, “r”)
  • text = read(file)

18. You want to check for unusual login activity. Specifically, you want to read a log file that contains information on each login attempt, including whether it failed or was successful. You should then parse the data into a logins list, and then you should separate all failed log entries into a separate failed_logins list. If you want to automate this through Python, what would be part of your code? Select three answers.

  • An if statement to check if a login attempt failed
  • A for loop to iterate through all items in the logins list
  • A counter variable to keep track of the number of failed logins
  • A split() function to split the login information into a list

Shuffle Q/A 2

19. You included username_list[10] in your code, but username_list only contains five elements. What type of error is this?

  • Logic error
  • Exception
  • Name error
  • Syntax error

20. If you know there is a logic error somewhere inside a function, how can you figure out the exact location?

  • Place print statements in and around the function
  • Move the function to another location
  • Delete the function from the program
  • Write comments in and around the function

Leave a Reply