Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
The community reviewed whether to reopen this question 2 months ago and left it closed:
Original close reason(s) were not resolved
Improve this question
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")
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).
You haven't defined a variable true. Maybe you meant the built-in boolean value True?
while True:
# but seems like inifite loop
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 months ago.
Improve this question
I have been working on an operating system inside of Python and I am starting off with the system booting code. I have a while loop that executes when the variable "Booting" = 1. Inside the while loop is a script that prints "Booting" then replaces that with "Booting.", then it gets replaced with "Booting.." and so on until it reaches "Booting....." which should make it reset to "Booting" and reset the cycle. Instead, it just stops at "Booting....." and doesn't continue to reset the cycle.
Here is the code:
import time
import sys
Booting = 1
while Booting == 1:
print("Booting")
sys.stdout.write("\033[F")
time.sleep(0.2)
print("Booting.")
sys.stdout.write("\033[F")
time.sleep(0.2)
print("Booting..")
sys.stdout.write("\033[F")
time.sleep(0.2)
print("Booting...")
sys.stdout.write("\033[F")
time.sleep(0.2)
print("Booting....")
sys.stdout.write("\033[F")
time.sleep(0.2)
print("Booting.....")
sys.stdout.write("\033[F")
time.sleep(0.2)
it does not erase the rest of the line, so you need to replace any existing variables you want to overwrite with spaces
try
...
print("booting ")
...
print("booting. ")
...
etc
(there are many ways to clear the line this is just one (#code provides another good alternative in the comments)
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 3 years ago.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Improve this question
I am trying to make a calculator. I have the code that does the operations working, but I am trying to allow the user to keep the calculator running without having to rerun the code. i an trying to assign a Boolean to variable, but python keeps telling me there is a name error, which is the the variable is not defined. Can you please help me?
Thank you in advance.
I have tried to change the available name but it doesn't do anything.
The code stopped working when it got to the while run == true line.
else:
print('Invalid operator, please run code again')
run = True
while run == True:
print(' do you need another problem solved? y/n')
if input() == y:
run = True
elif input() == n:
run = False
I expected the code to ask me if I need another problem solved, but there is a name error.
If the error is complaining about y or n it's because you need to surround it with quotes
if input() == "y":
run = True
Also, run == True is not needed
while run:
does the trick
else:
print('Invalid operator, please run code again')
run = True
while run:
print(' do you need another problem solved? y/n')
inp=input()
if inp == 'y':
run = True
elif inp == 'n':
run = False
you have used input 2 times so your code will wait 2 times for input, use input() once.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
I'm new to Python and a little confused with the whole nonlocal thing. Here's the problem snippet of code:
positon = 0b0
while True:
pos_choice = input("\tPlease enter your choice: ").lower()
if pos_choice == '1':
position = position | Baseball.pitcher
break
elif pos_choice == '2':
position = position | Baseball.catcher
break
elif (pos_choice == 'd') and (position != 0b0):
break
elif (pos_choice == 'd') and (position == 0b0):
print("\tChoose a position.")
else:
print("Invalid choice.")
print(position)
So this throws me :
Traceback (most recent call last):
File "driver.py", line 252, in <module>
load_student()
File "driver.py", line 142, in load_student
position = position | Baseball.catcher
UnboundLocalError: local variable 'position' referenced before assignment
Based upon what I've read on answers to other questions, the problem would be because the problematic "position" is nested two loops in from the original call (is that right?).
My main problem is that I can't figure out how to bind the two "position"s using nonlocal, though I have tried various solutions to no avail. Also, is using nonlocal a taboo like it is when using global? Thanks for the help!
You have a typo in your code. In the declaration, the variable is spelled positon instead of position.
There is a simple typo in your first line.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
hey I need help with a stupid syntax error using the if and else statement.
GNU nano 2.2.6
#!/usr/bin/python
print 'ACTIVATED'
import RPi.GPIO as GPIO ## Import GPIO library
GPIO.setmode(GPIO.BOARD) ## Use board pin numbering
GPIO.setup(40, GPIO.IN) ## Setup GPIO Pin 40 to OUT
GPIO.input(40) ## Turn on GPIO pin 40
for x in xrange(10):
if: GPIO.input(40)
print ('CHEESE')
else:
GPIO.cleanup()
heres the error:
File "./gid.py", line 12
if: GPIO.input(40)
^
Your if needs to have a conditional
if: GPIO.input(40) # wrong placing of semicolon with missing conditional
it has to be
if GPIO.input(40): # correct usage
as GPIO.input(40) returns a boolean
(Apart from that your print indent has mismatched)
Perhaps you mean if GPIO.input(40):. The colon goes after the entire if condition.
There are two things wrong:
if: GPIO.input(40) should be if GPIO.input(40):
You need to define something to happen inside the if statement. For example:
if GPIO.input(40):
print('CHEESE')
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I'm new to Python and I'm trying classes and objects, I have this script:
#!/usr/bin/env python
class test:
def __init__(self, username):
self.username = username
def name_again(self):
for i in range(0-4):
print ("username is %s" %self.username)
ahmed = test('ahmbor')
ahmed.name_again()
I'm expecting this script to print "username is ahmbor" 5 times
When I run this script, I have nothing
Please help find what's wrong with this
You are telling range() to loop over 0-4 (subtract four from zero), which is -4. Because the default is to start at 0 and count up, that is an empty range:
>>> range(0-4)
range(0, -4)
>>> len(range(0-4))
0
Use a comma instead, and use 5 to loop 5 times, not 4. The endpoint is not included:
>>> len(range(0, 4))
4
>>> len(range(0, 5))
5