Python Calculation for typed characters

Asked 2 years ago, Updated 2 years ago, 16 views

str=input("input")        

count=0

for i in str:

    ???

A or B is entered (for example, AAABBABABAB or BBBAAABABABA).
I want to calculate it like the count value 12323454321
If the same character appears consecutively, increase the count value by 1 and decrease the count value by 1 if the character is wrong I want to make a for door, but it's hard because I'm a Ask how to construct a for statement...

python

2022-09-21 11:10

1 Answers

string = input ('input')
pre = string[0]
count = 0
for s in string:
    if s == pre:
        count += 1
    else:
        count += -1
        pre = s
    print(count, end='')


2022-09-21 11:10

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.