This question already has answers here:
Determine whether integer is between two other integers
(16 answers)
Use two or more relational operators in one sentence in python
(2 answers)
Closed 4 years ago.
Running python v3.6.5 PyCharm
I have a simple for loop with an if and else statement with it:
for i in range(10):
if i > 3 < 5:
print(i, "first")
else:
print(i, "second")
The output I get is:
0 second
1 second
2 second
3 second
4 first
5 first
6 first
7 first
8 first
9 first
but shouldn't the output be:
0 second
1 second
2 second
3 second
4 first
5 second
6 second
7 second
8 second
9 second
Try it yourself. It doesn't make sense to me. Why is it doing this?
Don't really need to know
I know you might be thinking, why didn't you just say
if i == 4
but this is just a simplified problem in my program.
Thanks in advance
Hugo
A chain of operators like i > 3 < 5 is interpreted as
i > 3 and 3 < 5
where the "middle" operand(s) are repeated for the left and right operator. You want
3 < i and i < 5
, which can be abbreviated (using the reverse of the previous interpretation) as 3 < i < 5.
The correct syntax is:
if 3 < i < 5:
Be aware that Python is special here, and this construct won't work in most other languages (where you'd have to say something like 3 < i and i < 5 instead).
Related
This question already has answers here:
Print a list of space-separated elements
(4 answers)
Closed last year.
I have recently started learning Python and wanted to try bubble sort. I'm getting the desired output, but is there a way to not have the space after the final element?
My Output
(0 1 2 4 4 5 7 9 14 18 18 )
The output I want
(0 1 2 4 4 5 7 9 14 18 18)
Ignore the brackets, they are to show the space
def bubble(arr):
n=len(arr)
for i in range(n-1):
for j in range(0,n-i-1):
if arr[j]>arr[j+1]:
arr[j], arr[j+1]= arr[j+1], arr[j]
for k in range(n):
print(arr[k],end=" ")
You can use transform each integer into a string using str(), then use " ".join to avoid having to print the trailing space.
print(" ".join(str(item) for item in arr))
This question already has answers here:
Changing the value of range during iteration in Python
(4 answers)
Closed 2 years ago.
I am coming from a c++ language and there if i change the value of 'i' in the loop then it affect the iteration of the loop but it is not happening in python.
for example, in c++:
for(int i=0; i<10; i++){
cout<<i<<" ";
if(i==5)
i = 8;
}
in above code, as the value of 'i' reaches 5 it become 8 and after one more iteration it become 9 and then loop ends.
output of the above code is-
0 1 2 3 4 5 9
but when i write the similar code in python it doesn't affect the iteration and the loop runs all 10 times.
code in python is -
for i in range(0, 10):
if i == 5:
i = 8
print(i, end=" ")
the output of this code is -
0 1 2 3 4 8 6 7 8 9
it is just changing the value of 5 to 8, and not changing the loop iteration.
how can i achieve the c++ result in python, please help!
thanks in advance :)
When you say i in range(0,10) you are generating a list of [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] that you are later on iterating over, this means that the value of i is not used as an increasing int but you are rather iterating over an array.
The best solution would probably be to change from a for loop to a while loop instead.
i = 0
while i < 10:
if i == 5:
i = 8
print(i)
i += 1
You could try a while loop:
i = 0
while i < 10:
if i == 6:
i = 9
print(i)
i += 1
Output:
0 1 2 3 4 5 9
This question already has answers here:
Scope of python variable in for loop
(10 answers)
Closed 3 years ago.
I want to print i and k till i is less than or equal to k.
in C++ the code can be given as:
for(i=0;i<k;i++){
cout<<i<<k;
k--;
}
I am not getting the correct output.
this is my code
k=5
for i in range(k):
print(i,k)
k-=1
the output i get is:
0 5
1 4
2 3
3 2
4 1
but i want to get:
0 5
1 4
2 3
is there someway to use the range() function for this?
For loops in Python are really for-each and suboptimal for your needs. Use while instead:
i = 0; k = 5
while i < k:
print(i,k)
i += 1
k -= 1
k=5
for i in range(k):
print(i,k)
if k<=i:
break
k-=1
This question already has answers here:
How does the modulo (%) operator work on negative numbers in Python?
(12 answers)
Closed 4 years ago.
for instance,-10 % 3 = 2, this does not make any sense as the definition for % is remainder. Thanks
-10 = 3(-4)+2, hence the remainder is 2 mathematically.
Alternatively notice that that 10 % 3 is equal to 1. Hence the remainder of -10 % 3 should be -1 which is equal to 3-1 = 2 modulo 3.
By the rule that a % b follows the sign of b and |a%b| < b, 2 is returned as the answer.
I wrote the following two functions:
def place_walls(level):
wall_cords = level[2].split("\n")
for cord in wall_cords:
process_coordinate(cord)
def process_coordinate(coordinate):
cords = coordinate.split()
print cords[0]
Python returns a list index out of range error. If I print the cords variable without an index it prints all the cords in a list, however the last list is an empty list. Might that be the problem, how could that be solved?
The input level looks like this:
1 0
0 0=r=3 3
4 3
5 3
6 3
7 3
8 3
9 3
10 3
11 3
3 4
Assuming it is just the last, empty list you have a problem with: clearly, there is no 1st element to print in that case, so wrap it in an if statement (for example if len(cords)>0:; if the list is empty, do whatever is more appropriate for that circumstance.