A desktop administrator is troubleshooting a custom administrative Python script (`.py`) designed to monitor workstation resource utilization. The script fetches a threshold limit from a system environment variable using `os.environ.get("MAX_RAM_MB")` and evaluates it against an integer representing available system memory (`free_ram`). During testing, the script crashes with the error: `TypeError: '<' not supported between instances of 'int' and 'str'`. Which of the following is the BEST resolution to correct this script error?
- Convert the environment variable value from a string to an integer data type using the int() function prior to comparison.Cevap
- BEnclose both variables in double quotes within the conditional statement to trigger automatic data type coercion.
- CAppend the /i command-line switch to the environment variable retrieval statement to force numerical parsing.
- DExecute the chmod +x command on the script file to grant numeric evaluation privileges to the script interpreter.
Cevap
Convert the environment variable value from a string to an integer data type using the int() function prior to comparison.
System environment variables return values as string data types. In Python, comparing an integer directly against a string using relational operators causes a runtime `TypeError`. Explicitly casting the string output to an integer using `int()` resolves the data type mismatch and permits accurate numeric comparison.
Adım Adım Çözüm
Anahtar Kavram
Environment Variable Data Types and Type Casting in Scripts
Tahmini Süre:2m 0s