Increasing the value of x in a for loop

Viewed 84

I'm trying to edit the value of bigalgs, but when I actually print the value of it, it just spits the initial value of bigalgs, which is 0. Why is this happening? Am I increasing the value of bigalgs the wrong way? I think it was by simply using the + sign... Maybe the for loop? Help!

(for reference, alg here means algorism)


class scinoteConvert:
    def __init__(self, num):
    
        alglog = []
        alglist = []
        bigalgs = 0
        num = str(num)

        for alg in num:
            alglist.append(alg)

        for alg in alglist:
            if int(alg) < 1:
                alglog.append(True)
            else:
                alglog.append(False)
                print(bigalgs)
                bigalgs + 1

        print(alglist, alglog, bigalgs)

input = int(input('Insert num: '))

if input < 1:
    exit('Error! Cannot calculate negative values!')

scinoteConvert(input)
3 Answers

I think you should write bigalgs = bigalgs + 1 instead of bigalgs + 1. When you write bigalgs + 1 you do not change the value of the variable.

Your code is correct , But you should be in the syntax to add 1

bigalgs += 1

the code will become :

class scinoteConvert:
def __init__(self, num):

    alglog = []
    alglist = []
    bigalgs = 0
    num = str(num)

    for alg in num:
        alglist.append(alg)

    for alg in alglist:
        if int(alg) < 1:
            alglog.append(True)
        else:
            alglog.append(False)
            print(bigalgs)
            bigalgs += 1

    print(alglist, alglog, bigalgs)



input = int(input('Insert num: '))

if input < 1:
    exit('Error! Cannot calculate negative values!')

scinoteConvert(input)

You have to use += 1, so bigalgs + = 1, so your solution is

bigalgs =  bigalgs +=1 

because +=1 is for incrementing a variable by one in a loop, so modify the object in-place if it's mutable. You only wrote bigalgs + 1.

Here's how to increase the bigals value the right way. That way when you print the bigals value, you shouldn't spit out its initial value 0.

class scinoteConvert:
    def __init__(self, num):
    
        alglog = []
        alglist = []
        bigalgs = 0
        num = str(num)

        for alg in num:
            alglist.append(alg)

        for alg in alglist:
            if int(alg) < 1:
                alglog.append(True)
            else:
                alglog.append(False)
                print(bigalgs)
                bigalgs =  bigalgs +=1

        print(alglist, alglog, bigalgs)

input = int(input('Insert num: '))

if input < 1:
    exit('Error! Cannot calculate negative values!')

scinoteConvert(input)
Related