Ask coding questions

← Back to all posts
How to get the length of an integer?
CaydenOkeeffe (1)

Look, Im trying to create a 3 digit PIN cracker, but I cant even get past the input validation stage as whenever I try to use len(Acc_Code) it always returns 0. This means that no matter the length, it will always continue. Pls help.

Answered by OldWizard209 (1093) [earned 5 cycles]
View Answer
Comments
hotnewtop
OldWizard209 (1093)

Yeah u gotta do this:

Acc_Code = int(34)

def change_into_list(string): 
    list_of_string = [] 
    list_of_string[:0] = string 
    return list_of_string

string_= str(Acc_Code)
length = len(change_into_list(string_))
print(length)

This turns the string into a list and gets its' length.

Coder100 (16985)

You can just set the variable instead of doing all that globals nonsense. It's already captured by the function closure:

Acc_Code = input("enter code to be cracked")

also you need to set it as a str, don't type cast to int or else it won't be able to get the length. Only strs have length. Ints don't.

MuffinsTheCat (18)

You can use the len(*object*) function to get the length of an object. For example:

x = 200
y = len(x)
print(y)

The output will be:

3
OldWizard209 (1093)

No sir, the len() function is only for lists not for str and ints... @MuffinsTheCat

MuffinsTheCat (18)

@OldWizard209 Alright I did some testing. Try this:

x = 4827
print(len(str(x)))

The result will be 4

OldWizard209 (1093)

ok the code you wrote before was wrong, this one is correct... so we have a point and a mistake each... :) @MuffinsTheCat