Doing something after for loop finishes in same function? [closed] - python

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 8 years ago.
Improve this question
Is this possible? I'm doing an bukkit plugin now (in Python, yes :D), and I'm forced to do this within one function, so I can't separate it and call it later... For example, if I have loop that loops through players on server and adds everyone except one player, I want it to finish, and then teleport i.e. "Player1" to random player. At the moment, it teleports "Player1" to random player every time because of for loop... I'll give you just little of code, since It looks messy in preview due to many things that are not involved in problem and could be confusable to you... Here it is:
listica = []
for p1 in org.bukkit.Bukkit.getWorld(nextvalue).getPlayers():
if p1.getName() not in listica:
try:
listica.remove(event.getPlayer().getName())
randomtarget = choice(listica)
randomtargetreal = org.bukkit.Bukkit.getPlayer(randomtarget)
event.getPlayer().teleport(randomtargetreal)
event.getPlayer().sendMessage("%sYou teleported to: %s%s"% (bukkit.ChatColor.GREEN, bukkit.ChatColor.DARK_GREEN, randomtarget))
except ValueError:
randomtarget = choice(listica)
randomtargetreal = org.bukkit.Bukkit.getPlayer(randomtarget)
if event.getPlayer().getLocation() != randomtargetreal.getLocation():
event.getPlayer().teleport(randomtargetreal)
event.getPlayer().sendMessage("%sYou teleported to: %s%s"%(bukkit.ChatColor.GREEN, bukkit.ChatColor.DARK_GREEN, randomtarget))
What I want is:
run for loop:
when there is no more players to add a.k.a it finishes
do try loop
P.S. I can't do it in separate function.
Thanks in advance! :)

Do you mean:
def func(args):
for item in loop:
do something
try: # note indentation
something else

Related

How do I assign multiple strings to one variable? [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 1 year ago.
Improve this question
So I have tried searching this but apparently nobody ever needed to do this simple thing before?
I want to a variable to have multiple strings. so basically it is:
command = input()
commands = "start" or "stop" or "help"
while command.lower() == commands:
dosomething()
else
dosomething()
This is basically the idea, but it only take the first string which is "start" but ignores the other 2. I understand that it reads it as ( commands = "start" ) so I tried making it
commands = "start" or commands = "stop" or commands = "help"
but it flat out says it is wrong. so what did I do instead?
Can someone help? Thanks
What you need is a list, then check for inclusion with if (not while which is a loop)
command = input(">>")
commands = ["start", "stop", "help"]
if command.lower() in commands:
print("start/stop/help")
else:
print("other")

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

Close Only One Chrome Tab on TaskBar [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 2 years ago.
Improve this question
I have 2 chrome tabs like that:
Tabs
I want to close one of them.I will use python to close.How can I close?
Using psutil module this can be achieved
Given code will kill first instance of process found. (If you want to kill all instance you can remove/comment return statement inside try block)
import psutil
def kill_process_by_name(processName):
# Iterate over all running processes
for proc in psutil.process_iter():
# Get process detail as dictionary
pinfo = proc.as_dict(attrs=['pid', 'name', 'create_time'])
# Check if process name contains the given name string.
try:
if processName.lower() in pinfo['name'].lower():
print(pinfo)
p = psutil.Process(pinfo['pid'])
p.terminate()
return
except Exception as e:
pass
#ignore any exception
return
kill_process_by_name( processName='chrome')

How do make text in print() and input() appear one by one? [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 3 years ago.
Improve this question
Almost like an RPG game, I want to make text appear as if someone is typing them. I have an idea of how to do it with the print() function in python, something involving the sleep() and maybe with sys.stdout.flush?
How would I do it text coming before an input function?
For example, I want What is your name? to be typed out, and then the user would input his name.
You can use this:
text = 'What is your name? '
for x in text:
sys.stdout.write(x)
sys.stdout.flush()
time.sleep(0.00001)
name = input()
you can randomize the sleep time per loop as well to mimic typing even better like this:
import time,sys,random
text = 'What is your name? '
for x in text:
sys.stdout.write(x)
sys.stdout.flush()
time.sleep(random.uniform(.000001, .000019))
name = input()
as Tomerikoo pointed out, some systems have faster/slow delays so you may need to use uniform(.01, .5) on another system. I use OS/X.
On windows this probably works better. Thanks Tomerikoo:
import time,sys,random
text = 'What is your name? '
for x in text:
print(x, end="", flush=True)
time.sleep(random.uniform(.000001, .000019))
# or smaller sleep time, really depends on your system:
# time.sleep(random.uniform(.01, .5))
name = input()
You can use the following code:
import time,sys
def getinput(question):
text = input(question)
for x in text:
sys.stdout.write(x)
sys.stdout.flush()
time.sleep(0.00001) #Sets the speed of typing, depending on your system
Now everytime you call getinput("Sample Question"), you would get the user's input based on the question you passed to the function.

Call function at any time on input, python 3.x [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 5 years ago.
Improve this question
If you made a function like this:
def show():
print(something)
How would you make it so that, on any input, if the user typed a specific thing, this would be called? I want multiple functions like this to be able to be called whenever the user wants. Would I just have to have it as an option every time I ask for an input?
EDIT: sorry, this wasn't very clear. Say I have a variable in a game, like money. When I ask for an input as the game goes along, the inputs being about unrelated things, I want the user to be able to type eg. gold and the show() function will activate. Then the input will go on as usual. Would I be able to do this without just have it as an option for each input, eg.
variable = input("type something")
if variable == "gold":
do stuff
elif variable == other things
do other things
Do you mean something like this:
def thing1(): # Random command
print('stuff')
def thing2(): # Random command
print('More stuff')
def check(command):
'''Checks if the users command is valid else it does a default command'''
if command == 'thing1':
thing1()
elif command == 'thing2':
thing2()
else:
default_thing()
while True: # Loop going on forever
userinput = input('') # Lets the user input their command
check(userinput)
you could put all your functions in a dictionary: {"<some user input>":func_to_call} and see if it matches any
something like:
def gold():
print("gold")
input_options = {"gold":gold}
variable = input("type something:")
if variable in input_options:
input_options[variable]()

Categories