How to break loop and then start over [closed] - python

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"

Related

Somebody help me understand this ctypes python code? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 1 year ago.
Improve this question
hotkey_ = enable_shortcut # Check to break the while loop
def proper_fctn():
if hotkey_:
if not user32.RegisterHotKey(None, shortcut_id, modifier, vk):
pass
try:
msg = wintypes.MSG()
while user32.GetMessageA(byref(msg), None, 0, 0) != 0:
if msg.message == win32con.WM_HOTKEY:
if not hotkey_:
break
fctn_to_run()
user32.TranslateMessage(byref(msg))
user32.DispatchMessageA(byref(msg))
except:
pass
If somebody could help me understand the lines, so I could understand the process better.
These are Win32 APIs. All of them are well-documented in Microsoft's MSDN documentation. You've basically written a Windows application with a standard main loop.
Google takes you straight to their docs.
https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-registerhotkey
https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-getmessage

Not instantly getting out a loop with break [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
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.
Closed 1 year ago.
Improve this question
When I run this easy program and want to exit it, I need to type exit 2 times (at line 7).
def registration():
def username_registration():
entering_username = True
while entering_username:
print("Type exit to leave the registration.")
usernam_from_registration = input("Enter username: ")
if usernam_from_registration == "exit":
break
lenght_usernam_from_registration = len(usernam_from_registration)
if lenght_usernam_from_registration > 15:
print("too long")
else:
return usernam_from_registration
username_registration()
print(username_registration())
registration()
Why is this and how can I make it so I only need to write it one time?
This is because you are calling the username_registration() function twice.
username_registration()
print(username_registration())
The first time you call it, nothing happens because you are not doing anything with the result.

How does using an if statement after an elif statement work? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
This simply a concept question. In a chain of if statements what would happen in the following chain? if-elif-if. I am not sure how the code will flow with the if statement after the elif.
If there is an if statement after an elif, this means this a different block of if statements. For example, if I do
yes = "yes"
no = "no"
if yes == "yes":
print("yee")
elif no == "no":
print(":(")
It will only print "yee" as after it stopped after the first one worked. However, if we do this:
if yes == "yes":
print("yee")
if no == "no":
print(":(")
Both will print as they are two different statements with no correspondence to each other.

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