An IT administrator is writing a Python (.py) script to automate system memory auditing across network workstations. The script imports a memory limit value from a text configuration file as "16", but when the script attempts to add 4 to this value, it throws a type error and fails to execute the calculation. Which scripting construct and method should the administrator use to resolve this issue?
- Perform data type casting by wrapping the variable in the int() function to convert the string into an integer before calculation.Answer
- BPrefix the variable with the $env: scope modifier to instruct the interpreter to treat the value as an environment integer.
- CAppend the /f command switch to the variable declaration line to force numeric parsing.
- DExecute the chmod command within the script to change the file permission of the input variable to an executable integer.
Answer
Perform data type casting by wrapping the variable in the int() function to convert the string into an integer before calculation.
The correct answer identifies that input read from text files is treated as a string data type. In Python and similar scripting languages, attempting to perform mathematical addition between a string and an integer results in a type error. Explicitly converting (casting) the string to an integer using the int() construct allows numerical operations to succeed.
Step-by-Step Solution
Key Concept
Scripting Data Types and Explicit Type Casting
Estimated Time:1m 0s