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
Related
def xyz(a):
i=0
while i>=a:
if i==777:
return"Jackpot"
else:
return"Try again"
i+=1
The above when i am running gives me no output. unable to figure out what's missing
There are many errors!
If you return before increment, you'll never have an increment.
If you return after increment , no point of while loop.
Solution
Simple if else
def xyz(a):
if a==777:
return "Jackpot"
else:
# i+=1
# return "Try again"
return ("Try Again")
Since you did not provide any information about what exactly you are trying to do all I can do is give you some advise based on what I can infer from your code. Notice that you are returning a value meaning you stop the execution of the while loop at the first iteration always (if you are 100% certain about the implementation of the function and your desired output is 'try again' then you have to 'catch' the returned value of the function and print that). Also check the while condition. I is initialised at 0 so (for me at least) it would make sense to check if i is less than a (but again this comes from my intuition about the code).
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```
I have a piece of coding that is most basically a quiz. I had error handling so that if someone pressed a letter or anything other than a number, it would carry on with the quiz instead of letting the person attempt another question. (For example, if question 5 was 2+3 and they entered t, then it would carry on and not give them a different question for question 5).
I tried to update the coding, so it would loop:
def random_question():#defines function to get random question
count = 0#equates count to 0
for number in range (0,10):#makes the code generate the question 10 times
try:#the code makes it so the computer tries one thing and is the beggining
questionList = [random_sum,random_subtraction,random_times]#puts functions in a list to be called on
randomQuestion = random.choice(questionList)#calls on the list to generate the random questions
randomQuestion()#calls on variable
except ValueError:#if there is a value error or a type error it will notice it and try to stop it
print ("Please enter a number")#prints out the statement if there is a type error or a value error
else:
break
random_question()#calls on the function random_question
However, it comes up with a syntax error, and highlights the 'except' part next to the ValueError.
Any help as to why this is would be much appreciated.
Your except statement should have the same indention as your try statement. In your sample code it is indented an extra tab which would cause that error.
Python takes indention seriously and it is often the culprit. I'm unclear if your def line is a place holder or if this code is part of it, but if this code is part of the function definition you have other indention issues to worry about.
I'd suggest going through it carefully and making sure everything is lined up properly. The basic rule of thumb is, if something is part of something else, it is indented under it.
def something():
step 1
step 2
if (condition):
the true thing
else:
the false thing
while (something):
repeat something
this is not part of the function anymore
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!
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