How do I use a for loop in a range to compare items from a list? (converting python to ai2)

In a binary list of either 1s or 0s (success or not a success) I am trying to see the current streak for how many of the same item in a row is within a list. For example, if the last 3 attempts are all 0s, the current streak would be -3. If the last 4 attempts are 1s, the current streak would be 4. The working python code that I am trying to convert to MIT App Inventor is:

for number in range(1, len(mylist)):

if mylist[number] == mylist[number-1]:
    if current > 0:
        current += 1
    elif current < 0:
        current -= 1

else:
    if current > 0:
        current = -1
    elif current < 0:
        current = 1
print(current)

Please no personal info here.
Use PM instead.

There is a for each item in list block inbuilt, which will be helpful to you.

What is the [number]?

what is PM?

A Personal Message.
You will be allowed to send PM's when you are on Trust Level 1 (Basic).

See this

Trust Level 1 — Basic

At Discourse, we believe reading is the most fundamental and healthy action in any community. If a new user is willing to spend a little time reading, they will quickly be promoted to the first trust level.

Get to trust level 1 by…

  • Entering at least 5 topics
  • Reading at least 30 posts
  • Spend a total of 10 minutes reading posts

Users at trust level 1 can…

  • Use all core Discourse functions; all new user restrictions are removed
  • Send PMs
  • Upload images and attachments if enabled
  • Edit wiki posts
  • Flag posts
  • Mute other users

Admins can change these thresholds by searching for tl1 in site settings.

1 Like

I couldn't make your python code work, so I did this instead ...

Capture streaker.aia (4.4 KB) when btnClear Click

Since you only had 2 values (0,1) in your input stream, the 0s and 1s have their streak lengths in odd and even indices in the output list, depending on the value at the head of your input stream.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.