Conditional - Try something for x seconds [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 1 year ago.
Improve this question
This is my first question, as I have started to code recently.
here it goes...
I dont know how to do it, so I haven't coded it yet.
The idea is to have a Conditional to try doing something for x seconds, and if nothing happens on this x seconds, then do something to try it again.
Like this..
Try for 5 seconds to:
click on element to download something # <- this I know how to do
if nothing happens: # <- no error, just not executed the line above
refresh the page
try again:
finally:
You have your file
sorry for my English, as it is not my primary language and I am also learning it...

what web scraping tool you are using? selenium ? I can only give you my
logic if you do not post your code.
import datetime
def page_refresh():
print('refresh page')
driver.refresh()
def check_and_wait():
status_ready = False
while (not status_ready):
start_time = datetime.now()
while(not status_ready and datetime.now()-start_time<5): # loop 5 seconds
if(condtion == True): # if something happen
status_ready = True
return
page_refresh() # nothing happen , loop again

Related

My Tinder bot won't stop autoswiping right [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 2 years ago.
Improve this question
I want to avoid bot detection and my random stuff isn't working. Actually it's not hitting dislike at all. Xpaths are all right. What am I doing wrong here?
def auto_swipe(self):
while True:
sleep_time = random.randrange(1, 3)
time.sleep(sleep_time)
try:
rand1 = random.randrange(0,100)
if rand1 < random.randrange(70,80):
self.like()
else:
self.dislike()
except Exception:
try:
self.close_popup()
except Exception:
self.close_match()
The standard way to use random is as follows. Assuming you'd like something to happen 75% of the time, you'll write the following:
if random.random() < 0.75:
# do something

While loop not running indefinitely [python3]? [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 3 years ago.
Improve this question
I'm creating TUI (Text-based user interface) using print statements, and when I want to return to the 'homescreen' i want the older code to run again.
randbool = True
while randbool:
print('1')
randbool = False
while not randbool:
print('2')
randbool = True
the result im expecting is
1
2
1
2
1
2
....
but it only prints 1, 2 how can I make it run indefinitely?
Not advisable, but:
while True:
print('1')
print('2')
This will print 1,2,1,2,1,2 indefinitely, until your CPU usage is at 100%, and your whole system freezes.
But it will accomplish what you're asking for.
Edit to add: 100% CPU usage demonstrated on an i7 laptop with 16GB RAM on Ubuntu 18.04:
If the value of randbool lets you get in a loop, changing it will stop the loop.
So don't change it for a loop you don't want to stop.
If you want to print 1 and 2 indefinitely, a simpler solution would be:
# loop forever
while True:
print('1')
print('2')

Get input whilst looping 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 7 years ago.
Improve this question
I currently have a Twitter bot that streams tweets from Twitter in my timeline. Regardless of what it does, how could I also send Tweets (get keyboard input) at the same time as receiving Tweets (looping code).
This isn't a question about the Twitter API, just a general question on how to get input while looping code.
I'd create a specific thread for it, when used would call your Twitter API post function. (But it depends how your code is structured)
import threading
t1 = threading.Thread(target=post_from_keyboard)
t1.start()
t1.join()
# Loop exits when users writes quit.
# Obviously it won't post any Tweets with the word "quit"
def post_from_keyboard():
while(True):
kb_tweet = input("Enter Tweet or write "quit" to exit")
if kb_tweet != "quit"
your_tweet_api_call( kb_tweet )
else:
break
I understand that you don't want your loop to wait for you to enter a tweet in each iteration, so you can't use the most common method raw_input.
In this case, it is platform specific. For Unix systems, you should use the select module, while in Windows, you have the msvcrt one.
With select, the idea is to check stdin inside every iteration, and process the message when you get one.
Something like:
import sys
import select
while True:
message = select.select([sys.stdin], [], [], timeout=0.1)[0]
if message:
print(message)
Perhaps,you are looking for something like this
userInput = raw_input()
while(userInput != q):
#do something
userInput = raw_input()

How can i improve this code to run faster? [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 7 years ago.
Improve this question
I'm making The Powder Toy in Python, and I encountered some problems. Firstly the code runs VERY slowly. I know the problem is in my main file: http://pastebin.com/bbQ4H4Xu. The other files are just detecting input / creating the 2d array, so the problem isn't there.
Within my main file, the problem seems to be in the method updatescreen(). How can I increase the performance of this function?
import pygame
#inputkey.py
from pygame.locals import *
def input_key():
global inputt
inputt = ""
key = pygame.key.get_pressed()
if key[K_q]:
return 'q'
elif key[K_w]:
return 'w'
elif key[K_e]:
return 'e'
elif key[K_r]:
return 'r'
#Createblocks.py
blocks = []
for i in range(400):
blocks.append([])
for j in range(400):
blocks[i].append(0)
Your Main.py file has a print statement in the loop:
def updatescreen():
#The problem is here, it slows down the code.
for i in range(windh):
for x in range(windw):
print x, i # <== Here
if not blocks[i][x] == 0:
if blocks[i][x] == "Stone":
screen.blit(elementStone, (x,i))
presumably for debug? That's performing windw * windh = 2,500 print operations, which will slow the code down for sure. Try removing that and see how it improves.

How Can I Print Running Text? For Example to Print "Stack" Python Will Print S then t then a then c and then k? [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 8 years ago.
Improve this question
I am using notepad++ for writing codes and then seeing output using Windows Power Shell. Now lets say I want to print this...
"Stack Overflow"
But not all at once. First of all it will print S then t then a then c then k then a space and then O ...... ?
So is there any function/method to post the text as we type in old PCs?
If the question is not complete let me know what more information I can provide you.
Update
#uʍop ǝpısdn Yes, you understood my requirement. And many thanks for the answer. :)
Well I didn't know that it calls Emulating text. :)
Here you go:
import time, sys
for letter in "Stack Overflow":
sys.stdout.write(letter)
sys.stdout.flush()
time.sleep(1) # that's in seconds, adjust at will
sys.stdout.write('\n')
Why stdout.flush?
Output from a running program is usually buffered (i.e. held waiting) until certain conditions are met. The call to sleep() will not fulfill these conditions, and the text will not appear. By calling flush() after each letter, you force it out.
from __future__ import print_function # only needed for Python 2.x
import random
from time import sleep
def delay_print(s, min_delay=0.1, max_delay=0.8):
for ch in s:
delay = min_delay + random.random() * (max_delay - min_delay)
sleep(delay)
print(ch, end="")
print('')
delay_print("StackOverflow")

Categories

Resources