Help with python

  • Thread starter Thread starter raystriker
  • Start date Start date
  • Views Views 1,281
  • Replies Replies 8

raystriker

The powers that be
Member
Joined
Dec 28, 2011
Messages
1,528
Reaction score
654
Trophies
1
XP
2,684
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.
 
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
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!
 
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,
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,
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