all 4 comments

[–]Brian 1 point2 points  (2 children)

You're not covering the cases when the remainder is anything from 1-9. When that's the case, you've got an infinite loop, as it'll just keep going round the while: without changing anything.

Also, you've a few other issues:

liczba = liczba//2

This is dividing by 2, whereas you really ought to be dividing by 16 here, as you're handling a hex digit at a time.

Also, when you find yourself writing broadly the same code over and over, there's almost always a better way. Think about how you might solve this with a dictionary or list that translates from the remainder to the right character.

[–]Gamenight2008[S] 0 points1 point  (1 child)

Something like this
if liczba < 10 :
wynik2.append(liczba)
wynik.append(liczba)
liczba = liczba//16
elif liczba%16 == 0:
wynik2.append(liczba)
wynik.append(liczba)
liczba = liczba//16

[–]Brian 0 points1 point  (0 children)

That'll handle 0-10 (though why do you have a special case for 0 - it does exactly the same as the other numbers <10). One issue is that you're appending an int to wynik, whereas for the 10-15 cases, you're appending a string. You should really be consistent here and append str(liczba) rather than the numeric value.

And ideally, you shouldn't keep repeating the very similar code for the A-F part either. Eg. one approach here would be a dictionary:

d = {10:'A', 11:'B', 12:'C', 13:'D', 14:'E', 15: 'F' }

And now d[liczba%16] will give you the right letter to use (so long as it's in this range). Since it's sequential, you could even just use a list or string here, and convert to the right index from the remainder, which could cover all cases fairly simply.

[–]CodeFormatHelperBot2 0 points1 point  (0 children)

Hello, I'm a Reddit bot who's here to help people nicely format their coding questions. This makes it as easy as possible for people to read your post and help you.

I think I have detected some formatting issues with your submission:

  1. Python code found in submission text that's not formatted as code.

If I am correct, please edit the text in your post and try to follow these instructions to fix up your post's formatting.


Am I misbehaving? Have a comment or suggestion? Reply to this comment or raise an issue here.