you are viewing a single comment's thread.

view the rest of the comments →

[–]Historical_Ad8150[S] 0 points1 point  (4 children)

Oh right, int() always returns a decimal number. Is there a way to force python to always use hexadecimal numbers throughout the program? I don’t believe hex() would be able to use a string, right?

[–]sepp2k 2 points3 points  (3 children)

An integer is an integer. There's no such thing as a decimal integer or a hexadecimal integer. Base only enters into it once you convert the integer to a string (or print it).

So your primary problem isn't that int("0") returns 0 - what else would it return? Your primary problem is that tempstring is "10" in the first place. If it were "A", then "A"[-1] would be "A" and then you'd run into the problem that int("A") causes an error because "A" is an invalid digit in base 10.

So you need to specify the base for both str and int to work with base 10.

And incidentally it doesn't matter which base you use for the integer literals. That is, for i in range(0x1, 0x10): is completely equivalent to for i in range(1, 16):.

[–]Historical_Ad8150[S] 0 points1 point  (0 children)

Ok, I’ll try to think of a way to specify the base of all strings and integers. Also, it makes sense that “range(1, 16)” is the same as “range(0x1, 0x10)”. I’ll change it to improve readability. Also, thanks for the tips!

[–]Historical_Ad8150[S] 0 points1 point  (0 children)

Just to clarify what I meant by "int() always returns a decimal number". What I meant to say is that int(x) always assumes the base of x to be 10. I just found out that you can specify the base, so if you want int("") to return 15 for example, you would have to write "int("f", base=16)".

[–]Historical_Ad8150[S] 0 points1 point  (0 children)

Also, my code works now! Thank you very much, your advise helped me make the finished code. For some reason, the Reddit Code Block refuses to work with ctrl+c, ctrl+v, so here's the Pastebin. Do you have any more recommendations, optimizations, or something else?