Robert Grootjen
Robert Grootjen

Reputation: 197

Why is my code returning 0? And not the numbers of Upper and Lower characters?

I'm trying to code that calculates how many upper and lower characters in a string. Here's my code.

I've been trying to convert it to string, but not working.

def up_low(string):
    result1 = 0
    result2 = 0
    for x in string:
        if x == x.upper():
            result1 + 1

        elif x == x.lower():
            result2 + 1

    print('You have ' + str(result1) + ' upper characters and ' + 
str(result2) + ' lower characters!')



up_low('Hello Mr. Rogers, how are you this fine Tuesday?')

I expect my outcome to calculate the upper and lower characters. Right now I'm getting "You have 0 upper characters and 0 lower characters!".

It's not adding up to result1 and result2.

Upvotes: 0

Views: 33

Answers (2)

enriqueojedalara
enriqueojedalara

Reputation: 74

Seems your error is in the assignation, missimg a '=' symbol (E.g. result1 += 1)

for x in string: if x == x.upper(): result1 += 1

    elif x == x.lower():
        result2 +**=** 1

Upvotes: 1

Barend
Barend

Reputation: 17444

The problem is in the line result1 + 1 and result2 + 1. This is an expression, but not an assignment. In other words, you increment the counter, and then the incremented value goes nowhere.

The solution is to work the assignment operator = into there somewhere.

Upvotes: 1

Related Questions