Hi there i'm hoping someone can help me with this little problem. I'm pretty sure its a scope issue but since I'm new to python I cant say for sure.

Basically i'm trying to get the counter2 variables final result. Below is the function as it stands right now, I've tried pretty much everything i can think of so far any help would be really apreciated:

def Numero(name):
    
    
    alpha = { 'a': 1, 'b': 2, 'c': 3, 'd': 4}
    counter1 = 0
    counter2 = 0
    
    
    while counter1 != len(name):
        for key, value in alpha.items():
            if key in name[counter1]:
                counter2 += value

    counter1 += 1          
    
    return counter2

I'm pretty new to Python aswell, and am still very confused with the way for loops are structured (as opposed to Java or C++). I think the issue you have is with the indentation. The second to last line, 'counter1 += 1' should be within the while loop shouldn't it? Otherwise the loop will keep going and be infinite. If you just indent that line so that it's within the while loop, it should fix the problem. :)

Hey thanks for the reply. I've tried the code outside the function procedural style and it will work, its just inside this function it doesn't. I dont know if maybe the problem is the fact i'm reading in a list and trying to output an integer??

This is how your script should look:

def Numero(name):
    
    
    alpha = { 'a': 1, 'b': 2, 'c': 3, 'd': 4}
    counter1 = 0
    counter2 = 0
    
    
    while counter1 != len(name):
        for key, value in alpha.items():
            if key in name[counter1]:
                counter2 += value

        counter1 += 1  # Indented into the while loop
    
    return counter2

# This line calls the function and sets myFunc' to counter2
myFunc = Numero("abcd")
# Print the function to see what it equals...
print myFunc

shadwickman is right; you needed the indent. I'm guessing that your function hung in an endless loop except when name == ""?

Also, I'm guessing that your intent is that numero will return the total value of the name, counting 'a' as 1, 'b' as 2, etc.?

Python often -- very often! -- has slick ways of doing things. In this case, the .count() method is a slick way of optimizing your function:

def Numero(name):
    
    
    alpha = { 'a': 1, 'b': 2, 'c': 3, 'd': 4}
    counter2 = 0
    
    for char in alpha:    # run through possibles
       counter2 += alpha[char]*name.count(char)  # value * number of occurrences in name
    return counter2

# This line calls the function and sets myFunc' to counter2
myFunc = Numero("abcd")
# Print the function to see what it equals...
print myFunc

Python often -- very often! -- has slick ways of doing things. In this case, the .count() method is a slick way of optimizing your function:

Oh - that's quite a handy function! There are so many more little helpful things like count() in Python than there is in Java. Thank you for the enlightenment :)

Thanks a lot guys i didn't give ther indentation a thought since python never complained. I'll try your ideas out in the morning and write back if it worked or not. Either way i'm greatful for you's taking the time to reply so if i get it right the glory is on your heads.

Cheers and thank you :D

ok the indentation was the culprit. God do i feel like a fool. Thank you very much guys for helping a noob ! :)

This question has already been answered. Start a new discussion instead.