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,515
Trophies
2
Age
41
Location
Niedersachsen
XP
11,269
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,515
Trophies
2
Age
41
Location
Niedersachsen
XP
11,269
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,515
Trophies
2
Age
41
Location
Niedersachsen
XP
11,269
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
  • No one is chatting at the moment.
  • Quincy @ Quincy:
    Usually when such a big title leaks the Temp will be the first to report about it (going off of historical reports here, Pokemon SV being the latest one I can recall seeing pop up here)
  • K3Nv2 @ K3Nv2:
    I still like how a freaking mp3 file hacks webos all that security defeated by text yet again
  • BigOnYa @ BigOnYa:
    They have simulators for everything nowdays, cray cray. How about a sim that shows you playing the Switch.
  • K3Nv2 @ K3Nv2:
    That's called yuzu
    +1
  • BigOnYa @ BigOnYa:
    I want a 120hz 4k tv but crazy how more expensive the 120hz over the 60hz are. Or even more crazy is the price of 8k's.
  • K3Nv2 @ K3Nv2:
    No real point since movies are 30fps
  • BigOnYa @ BigOnYa:
    Not a big movie buff, more of a gamer tbh. And Series X is 120hz 8k ready, but yea only 120hz 4k games out right now, but thinking of in the future.
  • K3Nv2 @ K3Nv2:
    Mostly why you never see TV manufacturers going post 60hz
  • BigOnYa @ BigOnYa:
    I only watch tv when i goto bed, it puts me to sleep, and I have a nas drive filled w my fav shows so i can watch them in order, commercial free. I usually watch Married w Children, or South Park
  • K3Nv2 @ K3Nv2:
    Stremio ruined my need for nas
  • BigOnYa @ BigOnYa:
    I stream from Nas to firestick, one on every tv, and use Kodi. I'm happy w it, plays everything. (I pirate/torrent shows/movies on pc, and put on nas)
  • K3Nv2 @ K3Nv2:
    Kodi repost are still pretty popular
  • BigOnYa @ BigOnYa:
    What the hell is Kodi reposts? what do you mean, or "Wut?" -xdqwerty
  • K3Nv2 @ K3Nv2:
    Google them basically web crawlers to movie sites
  • BigOnYa @ BigOnYa:
    oh you mean the 3rd party apps on Kodi, yea i know what you mean, yea there are still a few cool ones, in fact watched the new planet of the apes movie other night w wifey thru one, was good pic surprisingly, not a cam
  • BigOnYa @ BigOnYa:
    Damn, only $2.06 and free shipping. Gotta cost more for them to ship than $2.06
    +1
  • BigOnYa @ BigOnYa:
    I got my Dad a firestick for Xmas and showed him those 3rd party sites on Kodi, he loves it, all he watches anymore. He said he has got 3 letters from AT&T already about pirating, but he says f them, let them shut my internet off (He wants out of his AT&T contract anyways)
  • K3Nv2 @ K3Nv2:
    That's where stremio comes to play never got a letter about it
  • BigOnYa @ BigOnYa:
    I just use a VPN, even give him my login and password so can use it also, and he refuses, he's funny.
  • BigOnYa @ BigOnYa:
    I had to find and get him an old style flip phone even without text, cause thats what he wanted. No text, no internet, only phone calls. Old, old school.
  • Psionic Roshambo @ Psionic Roshambo:
    @BigOnYa, Lol I bought a new USB card reader thing on AliExpress last month for I think like 87 cents. Free shipping from China... It arrived it works and honestly I don't understand how it was so cheap.
    +1
    Psionic Roshambo @ Psionic Roshambo: @BigOnYa, Lol I bought a new USB card reader thing on AliExpress last month for I think like 87... +1