Id like to write a for loop for the “bull retracement percentage” column where i <= 25. Then follow up with different less than and greater than scenarios. Every instance of this i want label it with different symbols. The while loop or If statement both do not work. it keeps saying TypeError: ‘<’ not supported between instances of ‘str’ and ‘int’
iching_trigram = []
for i in crude_df['bull retracement percentage']:
while i <= 25:
iching_trigram.append('--')
while i >= 26 or i <= 49:
iching_trigram.append('-o-')
while i >= 50 or i <= 77:
iching_trigram.append('-')
while i >= 78 or i <= 100:
iching_trigram.append('-x-')
print(iching_trigram)
TypeError: '<' not supported between instances of 'str' and 'int'
I have formatted the code snippet in the original post using back ticks to read better. You can click the edit button and see how I format the code. Formatting of code snippets helps you get a faster response because it is easier to follow and read.
How to format your code
Using three back ticks to format a block of code
```python3
for i in range(5):
print(i) ```
Then it becomes:
for i in range(5):
print(i)
Fixing TypeError
TypeError immediately tells us that the comparison in that expression are of different object types. Hence, the error.
That is, you are comparing between string and integer objects.
To fix it, you have to convert the string into integers.
i = int(i)
Infinite while loop
After you fix the TypeError error, you will have infinite loop problem since i is never increment within the while loop and the condition is always true.
Suppose i is less than 25. Then while i <= 25 is true. However, i is never increment. Therefore, the while loop runs forever since i <= 25 is always true.
To fix it, you have to increment i, but you may have index out of bounds depending on your implementation. See the next section how to improve the code without errors.
Improving the code
You don’t need a while loop since you are already iterating all elements in the list via the for loop.
Replace while with if , elif
The code is as follows:
iching_trigram = []
for i in crude_df['bull retracement percentage']:
i = int(i)
if i <= 25:
iching_trigram.append('--')
elif i >= 26 or i <= 49:
iching_trigram.append('-o-')
elif i >= 50 or i <= 77:
iching_trigram.append('-')
elif i >= 78 or i <= 100:
iching_trigram.append('-x-')
Now it is easier to read - for each element, assign a code according to the range.
We have a solved feature that allows you the ability to mark something as the “correct” answer, which helps future students with the same question quickly find the solution they’re looking for.
Here’s an article on how to mark posts as solved - I don’t want to do this for you until I know that solution/explanation works.We have a solved feature that allows you the ability to mark something as the “correct” answer, which helps future students with the same question quickly find the solution they’re looking for.
We have a solved feature that allows you the ability to mark something as the “correct” answer, which helps future students with the same question quickly find the solution they’re looking for.
Here’s an article on how to mark posts as solved - I don’t want to do this for you until I know that solution/explanation works.