How to streamline while loops for infinite cycle with pyautogui - python

I've lurked on SO for a little while, but all of the existing posts I've seen haven't been able to help me. I'm currently teaching myself python, so I apologize if this is an easy fix I'm not seeing.
My objective with this piece of code is to cycle through tabs in a browser using pyautogui.hotkey. It takes user input for number of tabs to cycle through, and executes the pyautogui command.
My issue, however, is that I can't manage to create a looping for or while loop.
I've played around with should_restart variables, for i in range(x) etc etc, but I'm just not seeing my fix.
The code below is essentially what I want to streamline together.
My idealized flow is:
Take input -> increase tabcounter by 1 until it equals input -> reset tabcounter-> rinse & repeat.
numberofTabs = input('How many tabs do you have? \n')
tabcounter = 0
while int(tabcounter) < int(numberofTabs):
tabcounter = tabcounter+1
pyautogui.hotkey('alt', str(tabcounter))
break
while int(tabcounter) == int(numberofTabs):
tabcounter = 0
I want this code to loop until I interrupt it, or for a lengthy period of time.
Thank you in advance. I appreciate the help!
EDIT: After reworking it, and wrapping my code in a loop, I came up with this:
loopcount = input('How many times do you want this to loop?')
time.sleep(5)
count = 0
for i in range(int(loopcount)):
while count < int(numberofTabs):
count += 1
pyautogui.hotkey('alt', str(count))
time.sleep(1)
else:
count = 0```

Related

The "continue" keyword in Python

Is there a way to count how many times the "continue" keyword (I don't know the proper term for it) is used in a for loop? I have a program that prints out "[>] Adding follower #" + str(index + 1)" after every successful loop. However, the follower number is inaccurate because sometimes the condition is met where the continue keyword is executed instead of the normal part of the loop. Can anyone help with this?
Without knowing your code is a little difficult; however this should be appliable to any for loop.
continue_count = 0
for x in my_iterable:
if some_condition:
continue_count += 1
continue

While loops and input functions are not working in python

I've been trying to work on practice modules in the python crash course book. I'm working on while loops and input functions. I tried running the below code in the terminal but only the second line of code prints.
What rental car would you like?
The full code is below:
while True:
car = input("What rental car would you like?")
print(f'Let me see if I can find you a {car.title()}.')
car += input('Subaru')
break
while True:
table = input("How many people are in your party?")
table = int(table)
if table > 8:
print('My apologies, you will have to wait for a table.')
else:
print('Your table is ready!')
break
I tried breaking the loops apart to get the rest to print in the terminal but I'm not sure where I'm messing up. I don't get any errors, it just only prints one line of code out of the entire thing.
It is waiting for your input. After you write something and hit enter it will proceed.
I did not quite understand the first while loop, but for the second one the problem is with the indentation of the break statement

Python Selenium Multithread Add Values From Each Thread Together

First of all idk if this is a duplicate question. I'm sorry if it is. But here is my problem.
I'm multi-threading and I want to add 2 values from each thread together. Here is my code for reference
def clicks():
driver = webdriver.Chrome("C:\\chromedriver.exe")
driver.get(#url)
place = #element of the place i want to click
count = 0
while True:
place.click()
count+=1
print(f'Clicks: {count}')
#for every click count + 1 and then prints the number of count
The above code works perfectly for single thread. But when I multithread it, this is the output :
Clicks: 50
Clicks: 102
I want to add the count from each thread together, I can't figure out a solution to do that. Would appreciate for any help, Thanks in advance.
Here is the full code: https://pastebin.com/Nm2dC74B

Looping a whole program in python using true and false statements

I have written a program but I do not know how to loop it. Help would be appreciated.
Here is the program I need help with.
There are two types of loops: indefinite (while) loops and definite (for) loops. If you want to loop your program a specific amount of times, then use the for loop:
for count in range(0, <number of repetitions minus one>):
# code
If you want to loop the program until the user enters "QUIT" or some other string, use this:
sentinel = input("Enter QUIT to exit or anything else to continue: ")
while sentinel.upper() != "QUIT":
# code
Here are some helpful links to tutorials:
http://www.learnpython.org/en/Loops
http://www.python-course.eu/python3_for_loop.php
http://anh.cs.luc.edu/python/hands-on/3.1/handsonHtml/whilestatements.html#while-statements
If you're talking about an infinite loop (i.e it has to go on and on without stopping) then simply use
white True:
<Your code here>
If it's not an infinite loop and only till a certain value comes true then you can do something like:
counter = 0
while counter <= 10:
print "This will continue till counter = 10"
counter += 1
You should make a function of the program, this function you can put in a loop. For further help, please post your code!

Python: Accept user input at any time

I am creating a unit that will do a number of things, one of them counting cycles of a machine. While I will be transferring this over to ladder logic (CoDeSys), I am putting my ideas into Python first.
I will have a count running, with just a simple
counter += 1
print("counter")
to keep track of what cycle I'm on. However, I want to be able to reset this count at any time, preferably by typing "RESET" I understand how to use the input command,
check = input()
however, I do not know how to let the program run while it is searching for an input, or whether or not this is possible at all. Thank you in advance for any answer.
If it helps to understand, here is the code. The big gap is where the problem is. http://pastebin.com/TZDsa4U4
If you only want to signal a reset of the counter, you can catch KeyboardInterrupt exception.
while True:
counter = 0
try:
while True:
counter += 1
print("counter")
except KeyboardInterrupt:
pass

Categories

Resources