51

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")
Hassan Faghihi
  • 1,888
  • 1
  • 37
  • 55
Jesper Andersen
  • 597
  • 1
  • 4
  • 7

3 Answers3

88

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).

poke
  • 369,085
  • 72
  • 557
  • 602
  • 8
    Sadly, in 3.x you can no longer do `__builtin__.True = False`, so without getting into `ctypes.pythonapi`, `true = False` is the best option left for confusing philosophy students. – abarnert May 07 '15 at 08:01
5

You haven't defined a variable true. Maybe you meant the built-in boolean value True?

phihag
  • 278,196
  • 72
  • 453
  • 469
3

while True:

# but seems like inifite loop

  • 1
    It is. This is a very common pattern for accepting a streaming input... `while (true) { input = get_input(stream); process(input); }` and so on. Something like an analogue signal will need to be processed as often as possible indefinitely. For instance, a self-driving car would run on an infinite loop over its cameras, radar, and other sensors and process the data thousands of times per second to be able to respond appropriately to what happens. – Christopher Reid Oct 19 '17 at 07:15