Help with python

raystriker

The powers that be
OP
Member
Joined
Dec 28, 2011
Messages
1,528
Trophies
1
XP
2,607
Country
India
So here's the thing, I want to convert a base 10 integer and output it in base 16 as a string-
Here's what I have:
Code:
#number n in base ten to base b=16
def d(n,b):
    ans=" "
   
    quotient=n/b

    while quotient !=0:
       
        remainder=(quotient % b)
        if remainder < 10:
            ans= str(remainder) + ans
        elif remainder == 10:
            ans= 'A' + ans
        elif remainder == 11:
            ans= 'B' + ans
        elif remainder == 12:
            ans= 'C' + ans
        elif remainder == 13:
            ans= 'D' + ans
        elif remainder == 14:
            ans= 'E' + ans
        elif remainder == 15:
            ans= 'F' + ans
       
        quotient=(quotient / b)
       
    return str(ans)
   
print(d(200,16))
print(d(689,16))
When I run the program, I get some weird output.
Where am I going wrong?
Plz help.
Thanks.
 

sarkwalvein

There's hope for a Xenosaga port.
Member
Joined
Jun 29, 2007
Messages
8,524
Trophies
2
Age
41
Location
Niedersachsen
XP
11,288
Country
Germany
Thanks, that worked!
Does my algorithm seem right?
My output is:
Code:
C
2B
but it should be:
Code:
C8
2B1
No, it is wrong because you are avoiding to check the last remainder.
You know, e.g.
If you did d(5), then the first check would do:
quotient = 5 // 16

and that is 0, so it will not get inside the while and deal with the 5, in other words it will print nothing.

If you want I can think of some suggestion to fix that.
 
Last edited by sarkwalvein, , Reason: LOL, I should really think out the suggestion. Of course what I suggested before doesn't work.
  • Like
Reactions: raystriker

raystriker

The powers that be
OP
Member
Joined
Dec 28, 2011
Messages
1,528
Trophies
1
XP
2,607
Country
India
No, it is wrong because you are avoiding to check the last remainder.
You know, e.g.
If you did d(5), then the first check would do:
quotient = 5 // 16

and that is 0, so it will not get inside the while and deal with the 5, in other words it will print nothing.

If you want I can think of some suggestion to fix that.
Thanks, I'll try and work something out!
 

raystriker

The powers that be
OP
Member
Joined
Dec 28, 2011
Messages
1,528
Trophies
1
XP
2,607
Country
India
No, it is wrong because you are avoiding to check the last remainder.
You know, e.g.
If you did d(5), then the first check would do:
quotient = 5 // 16

and that is 0, so it will not get inside the while and deal with the 5, in other words it will print nothing.

If you want I can think of some suggestion to fix that.
Seems that my first number's not being printed( under the first if statement), help?
 
Last edited by raystriker,

sarkwalvein

There's hope for a Xenosaga port.
Member
Joined
Jun 29, 2007
Messages
8,524
Trophies
2
Age
41
Location
Niedersachsen
XP
11,288
Country
Germany
Code:
#number n in base ten to base b=16
def d(n,b):
    if n==0:
       return str("0")
  
    ans=" "
    while n !=0:
   
        remainder=(n % b)

        if remainder < 10:
            ans= str(remainder) + ans
        elif remainder == 10:
            ans= 'A' + ans
        elif remainder == 11:
            ans= 'B' + ans
        elif remainder == 12:
            ans= 'C' + ans
        elif remainder == 13:
            ans= 'D' + ans
        elif remainder == 14:
            ans= 'E' + ans
        elif remainder == 15:
            ans= 'F' + ans
   
        # You are always printing the less significant digit of n
        # So iterative, you need to remove from n the less significant digit
        # (that is divide by the base), and start the loop again, until n is 0.
        n = n // b      # I know the variable name could be better thought, I leave that to you.

    return str(ans)
 
print(d(200,16))
print(d(689,16))

Try this.
 
Last edited by sarkwalvein,

sarkwalvein

There's hope for a Xenosaga port.
Member
Joined
Jun 29, 2007
Messages
8,524
Trophies
2
Age
41
Location
Niedersachsen
XP
11,288
Country
Germany
Also, I guess you are making this function just to try doing it yourself, otherwise you can use the standard function format to accomplish the same (e.g. format(200,'X') would do the same)
 

Site & Scene News

Popular threads in this forum

General chit-chat
Help Users
    K3Nv2 @ K3Nv2: https://youtu.be/qv96JYhfAuA?si=_PN4PTWfj5BWI9wk