The range function in python has the syntax:
range(start, end, step)
It has the same syntax as python lists where the start is inclusive but the end is exclusive.
So if you want to count from 5 to 1, you would use range(5,0,-1) and if you wanted to count from last to posn you would use range(last, posn - 1, -1).
The range function in python has the syntax:
range(start, end, step)
It has the same syntax as python lists where the start is inclusive but the end is exclusive.
So if you want to count from 5 to 1, you would use range(5,0,-1) and if you wanted to count from last to posn you would use range(last, posn - 1, -1).
In python, when you have an iterable, usually you iterate without an index:
letters = 'abcdef' # or a list, tupple or other iterable
for l in letters:
print(l)
If you need to traverse the iterable in reverse order, you would do:
for l in letters[::-1]:
print(l)
When for any reason you need the index, you can use enumerate:
for i, l in enumerate(letters, start=1): #start is 0 by default
print(i,l)
You can enumerate in reverse order too...
for i, l in enumerate(letters[::-1])
print(i,l)
ON ANOTHER NOTE...
Usually when we traverse an iterable we do it to apply the same procedure or function to each element. In these cases, it is better to use map:
If we need to capitilize each letter:
map(str.upper, letters)
Or get the Unicode code of each letter:
map(ord, letters)
Can you count the number of times a loop occurs?
"for i in range()" to do an infinite loop with a counter - Ideas - Discussions on Python.org
Python Count up & Down loop - Stack Overflow
Counting down within a text in a single line
Videos
Just trying to get the logic behind making a while loop make a counter go up then back down through the same sequence. I have no trouble making it go up then resetting it back to 0. I think I need some sort of construct to switch the counter from += to -= . I am hoping to get back into Arduino but when I originally tried I found that it was difficult because of the underlying C. There are some modules that allow interfacing to the Arduino so I'm hoping I can pick up where I left off and become more proficient in general with it (Eventually just going pure Python with a Raspberry Pi though)
https://gist.github.com/Nibby99/0a845a87f98714dae5d712d83c0fc078
I'm new to python, sorry if my wording is terrible.
I'm building a very basic number guessing game. I don't want to copy and paste the whole code into here unless someone wants it in the comments.
I have it so that you guess a number between 1-100. After each guess it will tell you if it's too low or too high. Once you guess it correct it will say "Correct!". I want it to also tell you how many attempts it took you to find the correct answer and print that out with the "Correct!". How do you do that?
print ("0-100")
for p in range (0,100): print ("seconds left"), print ( p )
wanted output
100 seconds left
99 seconds left
98 seconds left
thankful for any solution
Why does it has to be a while?
Maybe it is preference but for loops look cleaner.
Reversing can be done by using reversed function which reverses a iteration
countdown = 3
for count in reversed(range(1, countdown+1)):
print(count)
print('action!')
Or you could use the step parameter in range(start, end, step) and rewrite the for loop to
for count in range(countdown, 0, -1):
You can get rid of some instructions by writing this instead:
count_down = 3
while (count_down):
print(count_down)
count_down -= 1
print('Action!')
Note that I have replaced countDown by count_down to comply with PEP8' naming conventions.
Code explanation:
count_down -= 1 is equivalent to count_down = count_down - 1. You can read more on Python basic operators.
You do not need to check within the while loop if count_down reached the 0 value because it is already done when you coded while (countDown>=0). I mean you are duplicating the checking. In order to keep DRY, I just decrement the value of count_down by 1 and the break instruction is done by default as I am testing while(count_down) meaning if count_down != 0 in this context (because it also means while count_down is not False or None).