NameError: name 'true' is not defined

Jesper Andersen picture Jesper Andersen · May 7, 2015 · Viewed 67.4k times · Source

I want to use Boolean ( true / false ) in my python source file, but after running the application, I receive the following error:

NameError: name 'true' is not defined

The error lies on while true:, when I am trying to make the Raspberry Pi run a HTML script when it receives input on port 17:

import RPi.GPIO as GPIO
import time
import os

inputSignal = 17
GPIO.setmode(GPIO.BCM)
GPIO.setup(inputSignal,GPIO.IN)

while true:
    if (GPIO.input(inputSignal)):
        os.system("html /home/pi/index.html")
    else:
        print("No Input")

Answer

poke picture poke · May 7, 2015

Python’s boolean constants are capitalized: True and False with upper case T and F respectively.

The lower-case variants are just valid free names for variables, so you could use them for whatever you want, e.g. true = False (not recommended ;P).