for i in range(4): print(i) i += 1
How can I make this print:
0
2
How do I increment a counter inside a while test of Python - Stack Overflow
python - Increment and while loop - Stack Overflow
Help on where to put the increment function on while loops
So let me say this first. If
x = root**pwr
then
root = x**(1/pwr)
Does this help?
More on reddit.comNeed help understanding this while loop
Videos
The problem you can't do that way in Python is a restriction of Python syntax. Let's get what does while look like from documentation:
while_stmt ::= "while" expression ":" suite
["else" ":" suite]
As You can see you must put expression before ":", while x += 1 is a statement (and statements doesn't return any value so cannot be used as a condition).
Here's what does this code look like in Python:
i += 1
while a[i] < v:
if i == hi:
break
i += 1
Though it works, it's most likely not a Python way to solve your problem. When you have a collection and you want to use indices you have to look forward to redesigning your code using for loop and enumerate built-in function.
P.S.
Anyway, direct code porting between languages with absolutely different philosophies is not a good way to go.
The Java code sets i to the index of either the first element >= a[lo], or to hi, whichever appears first. So:
v = a[lo]
for i in range(lo+1, hi+1):
if a[i] >= v:
break
spam < 5 can be read as spam is less than 5, so it'll only increment from 0 to 4. In the 4th (last) iteration, spam = 4 so it prints 'Hello, world' and then spam + 1 = 5. At that point it will attempt another iteration, but spam < 5 is no longer true and therefore will exit the loop.
For reference: < means less than, <= means less than or equal to.
Any particular reason you think you'd get 6?
Your while loop is "while spam is less than 5", not "while spam is less than or equal to 5". The last iteration occurs when spam is 4, and then it gets incremented one last time to 5.
If spam equals 5, it is not less than 5, so the while loop stops iterating.
Please help me understand how the root and the pwr increments were placed down below on the following code
#
============================================================================= # Write a program that asks the user to enter an integer and prints two integers, root and pwr, such that 0 < pwr < 6 and root**pwr is equal to the integer entered by the user. If no such pair of integers exists, it should print a message to that effect. #
x = int(input('Enter an integer:'))
pwr = 1
while pwr < 6:
if x < 0:
root = x
else:
root = 0
while root**pwr <= x:
#print(root,'**',pwr)
if root**pwr == x:
print(root, '**', pwr, '=', x)
else:
if x**1 != x:
print('No such integer pairs exist')
root += 1
pwr += 1
So let me say this first. If
x = root**pwr
then
root = x**(1/pwr)
Does this help?
You're trying to understand the structure of the while loop? It's arranged like this:
while root**pwr <= x:
(check if this value of root is a solution)
(otherwise, increase root to get ready for the next attempt)
The condition root**pwr <= x will keep trying as long as root is small enough that it might be a potential solution. Once root**power > x, you know that you're never going to have a solution. Incrementing root any more will never get to a solution. So you give up at that point.
Why is the increment at the bottom? Think about the order of events. In a while loop, first it tests the condition, then if the condition is true it executes the code inside the loop. So you have this loop:
-
check if root is within the valid range (that's the while condition)
-
if so, check if root is a solution
-
increment root and go back to the top
So the next thing after incrementing is to check if root is in range and stop if not.
If you put the increment at the top of the loop, you'd have this:
-
check if root is within the valid range
-
if so, increment it (now it might not be in range any more!)
-
see if root is a solution.
-
go back to the top
You don't want to check invalid values. You want to stop as soon as root gets too big.
The other problem is what happens with the starting value, root = x or root = 0? If the first thing you did was increment before checking, then you never try those values. You immediately jump to x + 1 or 0 + 1 as the first thing to test.
for the code,
for i in range(0,10):
if i == 3:
i = i + 1
continue
print(i)
the output is going to be,
0
1
2
4
5
6
7
8
9
Breaking down the code,
for i in range(0, 10)
for loop runs for i=0 to i=9, each time initializing i with the value 0 to 9.
if i == 3:
i = i + 1
continue
print(i)
when i = 3, above condition executes, does the operation i=i+1 and then continue, which looks to be confusing you, so what continue does is it will jump the execution to start the next iteration without executing the code after it in the loop, i.e. print(i) would not be executed.
This means that for every iteration of i the loop will print i, but when i = 3 the if condition executes and continue is executed, leading to start the loop for next iteration i.e. i=4, hence the 3 is not printed.
In the provided code when you try to use i in a for loop with range, it always changes to the number provided by range in the function without bothering to look at the increment made to i. so basically if you try list(range(0, 10)) this will give you [0, 2, 3, 4, 5, 6, 7, 8, 9]. so for goes through that list one by one without thinking if any changes were made to i or not.
which if seen
loop_1: i=0
loop_2: i=1
loop_3: i=2
loop_4: i=3 (here now you increment value by 1), i=4
loop_5: i=4 (but the for loop was going though the list from range function so the change didn't do anything)
loop_6: i=5 (and so on until 9)