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
Related
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 months ago.
Improve this question
I tried for hours to solve this python riddle in my level of knowledge and I don't know what to write past the thinking part "I need to make tails more frequent by using +1 to something" maybe. The riddle goes like that:
import random
def broken_coin():
if random.random() <= 0.9:
return "Heads"
return "Tails"
using no random source part of the function(which you can't edit), and turn the coin into a standard one: if you print the code the chances for heads will be 50% and the chances for tails will be 50%
thanks early to anyone who commented :)
EDIT: added what was my idea
If I read your problem correctly, you need to provide a method to compensate for the broken coin producing a relatively fair amount of results. With that instead of calling the broken_coin() function every time directly, one could call a function that calls the function and every other time returns the reverse result to the calling function.
After reading the comments about what could or could not be used, I've updated my sample code.
import random
def broken_coin():
if random.random() <= 0.9:
return "Heads"
return "Tails"
def fix_coin(j):
if j == 0:
return broken_coin()
coin = broken_coin()
if (coin == "Heads"):
return "Tails"
else:
return "Heads"
for x in range (100):
print(fix_coin(x %2))
See if this more closely fulfils the spirit of the problem.
Regards.
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
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 3 years ago.
Improve this question
I have a function that scrapes websites and returns a statement that is depending if it found certain keywords. This function is called checksite. When I run the function on its own it works great but I can't get it to work inside another function together with time.sleep.
This works great
checksite()
This does not work
while True:
checksite()
time.sleep(10)
I want the checksite-function run every 10 second. All help is appreciated!
Your code should work. To check what is wrong, you could use this:
def checksite():
#blahblah
while True:
print('starting')
checksite()
print('site checked')
time.sleep(10)
print('sleep function complete')
Then maybe you will get an idea of what is wrong.
It is important to know what the checktime() execution does.
If you dont see anything happening after 10 seconds and the script still executing more than expected, my first suggestion would be to know how much time the execution takes.
You can run this and get the amount of time:
import time
import datetime
def checktime():
#Doing some execution
print('execution...')
#Use:
while True:
started = datetime.datetime.now()
checktime()
time.sleep(10)
executed = datetime.datetime.now()
print('The script runtime is: {0}'.format(executed - started))
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 4 years ago.
Improve this question
I wrote the parser log_file. But i can`t understand why I can not call a function in this block.
Help me please.
Python is dependant on indentation. In order for the program to work you need to add the correct indentation to your code. That involves 4 spaces for each loop or flow control.
Here are a few that do:
def
if
while
for
Your problem is that Python does not know where to end the while loop without indentation.
Here is an example:
for in range(5):
print('Working')
if i == 4:
print('yes')
else:
print('no')
There are two ways to indent this.
How does Python know whether the if statement should be in the for loop or not? Both the following are valid with very different results:
for in range(5):
print('Working')
if i == 4:
print('yes')
else:
print('no')
OR
for in range(5):
print('Working')
if i == 4:
print('yes')
else:
print('no')
In the first the while prints out the message 5 times and then the if statement starts.
In the second the if is part of the while loop so it runs 5 times as well as printing the message.
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")