(Python) Why is my while loop not working? [closed] - python

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)

Related

How to stop telegram bot running when i stop running python script? [closed]

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 2 years ago.
Improve this question
this is my first time using python to control telegram bot. I run this code below and would like to the bot to stop running. Could someone teach me how to make the telegram bot stop?
import time
import random
import datetime
import telepot
from telepot.loop import MessageLoop
def bot_test(msg):
chat_id = msg['chat']['id']
command = msg['text']
print('Got command:',command) #text from telegram
if command == '/roll':
bot.sendMessage(chat_id, random.randint(1, 2))
elif command == '/time':
bot.sendMessage(chat_id, str(datetime.datetime.now()))
bot = telepot.Bot('bot token')
MessageLoop(bot, bot_test).run_as_thread()
print ('I am listening ...')
while 1:
time.sleep(10)
The while loop never terminates as it's always true and is without any break statements. You could change it to like this:
while 1:
time.sleep(10)
break
Edit:
You also can change while loop to this:
while time.sleep(10):
break
Now it terminates the loop after 10 seconds, and by this way you can make your code shorter instead of writing while 1.

Uncaught Error: NameError: name 'true' is not defined [duplicate]

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

How to break loop and then start over [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
i want to ask how to break python while True loop in some point and then start it over again.
while True:
if a==0:
print "ok"
elif a==1:
break
#want to start over again
command = sock.recv(1024)
if blah blah ==blah:
.........
What you're looking for is probably continue
while True:
if a==0:
print "ok"
elif a==1:
continue
# goes back to top of loop
else:
print "ok"

Invalid syntax on if else statement [closed]

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 writing a simple text-based game and I've run into an error here.
def lamp():
print "You pick up the lamp and examine it."
print "It looks like an ordinary gas lamp."
print "What do you do?"
lamp_action = raw_input("> ")
if "rub" in lamp_action:
rub()
elif "break" or "smash" in lamp_action:
print "The lamp shatters into pieces."
dead("The room disappears and you are lost in the void.")
else:
lamp()
If I comment out the elif part, Python gives an invalid syntax error on else. If I leave the elif part in, the program will run without an error, but even typing something random like "aaaaaa" will follow the elif action.
I also doesn't work if I replace the else section with something like this:
else:
print "That's not a good idea."
lamp()
or like this:
else:
dead("That's not a good idea.")
Where dead is:
def dead(why):
print "%s Game over." % why
exit(0)
What did I miss?
"break" or "smash" in lamp_action is interpreted by Python as testing "break", then testing "smash" in lamp_action. Since "break" is a nonempty string, it is always interpreted as "true", so the elif is always taken.
The correct form is
elif lamp_action in ('break', 'smash'):
i.e. testing if the action is in a list of possibilities.

Python: while loop that quits program if not completed within specified limit [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 9 years ago.
Improve this question
Python Question: i need to run a program that asks for a password but if the wrong answer is input three times the user is thrown out of the program i can run it in a while loop but cant get it to quit if the wrong password is entered.
Thanks for your help
Adding an approximation of how I'd do it, in the absence of an example containing the problem. else on a for loop will only execute if you did not break out of the loop. Since you know the max number of times to run the loop is 3 you can just use a for loop instead of a while loop. break will still break you out early.
for _ in range(3):
if raw_input("Password:") == valid_passwd: # really should compare hashed values (as I shouldnt have passwords stored in the clear
print "you guessed correctly"
break
print "you guessed poorly"
else:
print "you have failed too many times, goodbye"
sys.exit(1)
# continue on your merry (they got the right password)
How about sys.exit()
>>> import sys
>>> guess = False
>>> if guess:
... pass
... else:
... sys.exit()
http://docs.python.org/3/library/sys.html

Categories