0

I'm trying to write a function in Python that will convert numbers into different bases. I created a while loop that should turn any number into a base 10 number. My plan is to convert the first number into base 10, then convert that into the target base. I'm doing it that way because all the arithmetic must be done in base 10.

I mapped out a flow chart and put the code in, but it always puts out the same result and I don't know why.

Here's my code:

def fconbase(cnum, cbase1, cbase2):
    #resets variables used in calculations
    exp=0
    result=0
    decimalResult=0
    currentDigit="blank"
    cnumlen=len(str(cnum)) #finds length of cnum, stays constant
    digitNum=cnumlen #sets starting placement
    while exp<cnumlen:
        currentDigit=str(cnum)[digitNum-1:digitNum]
        #the following converts letters into their corresponding integers
        if currentDigit=="a" or "A":
            currentDigit="10"
        if currentDigit=="b" or "B":
            currentDigit="11"
        if currentDigit=="c" or "C":
            currentDigit="12"
        if currentDigit=="d" or "D":
            currentDigit="13"
        if currentDigit=="e" or "E":
            currentdigit="14"
        if currentDigit=="f" or "F":
            currentDigit="15"
        result=int(currentDigit)
        decimalResult=decimalResult+result*(10**exp)
        exp=exp+1
        digitNum=digitNum-1
    print(decimalResult)

Here's how it should work:

First, it finds how many digits are in the original number. That is used to determine how many times the loop should continue.

Then it uses that to fish out the final digit. Next, it converts any letters into their appropriate digits (this is for if the number is in base 11 or higher). It turns that into an integer and multiplies it by 10^exp. The exp starts at 0, so 10^exp should come out to 1.

Then, it adds the result to the decimalResult (the result in base 10). Then it increases the exp(onent) by 1 so that 10**exp will now come out to 10 and subtracts one from the 'currentDigit' to fish out the second to last digit.

Then it loops around and continues this pattern until the exp is equal to the cnumlen (length of the starting number) and prints the final result.

I don't know where my formula is messing up. I don't see any logic errors in it that would result in this. I've been messing around with it for hours and can't get it to do anything different. Well, at first it was throwing out numbers that I had no clue where it was getting them. After messing around, now it just throws out the same number over and over. I can't figure out where its getting any of these numbers.

  • 3
    Hey, you can't do those "or" conditions like that. It's going to literally just evaluate ("A"), which is True. So your conditions will always be True. You need to do (currentDigit == "a") or (currentDigit == "A"). I.e. type it out in full. Better yet, do this comparison: currentDigit.upper() == "a".upper() – Neil Sep 16 '18 at 09:48
  • 1
    The line `if currentDigit=="f" or "F"` will always equate to true, so current digit will always be "15" as it's the last `if` evaluated... (see: https://stackoverflow.com/questions/20002503/why-does-a-b-or-c-or-d-always-evaluate-to-true) – Jon Clements Sep 16 '18 at 09:48
  • 1
    Not that the builtin `int` function takes a base argument (up to 36)... so you could do `int('ffeb109e', 16)` if 16 was your base and get your number that way. And there's builtin formatting to make a number binary, octal or hex representations so that'd cover outputs of bases 2, 8 and 16... (anything else you'd have to do yourself but that's not too tricky) – Jon Clements Sep 16 '18 at 09:51
  • 1
    simply use `int(value,base)` – soheshdoshi Sep 16 '18 at 09:53
  • try: `if currentDigit in "aA":` or `if currentDigit.lower()=="a":` or `if currentDigit =="a" or currentDigit=="A":` to test if a character is part of multiple values. - use `int( numberstring, base)` for you special purpose here – Patrick Artner Sep 16 '18 at 09:53
  • Also... if you're converting `cnum` to a `str` to get its digits - you won't get any characters not 0-9 as if it was passed as an int you've already got your base ten number... so the keep converting it to strings is a bit odd... – Jon Clements Sep 16 '18 at 09:55
  • Possible duplicate of [How to test multiple variables against a value?](https://stackoverflow.com/questions/15112125/how-to-test-multiple-variables-against-a-value) – Patrick Artner Sep 16 '18 at 09:55
  • Fixing the or statements fixed that issue. Also, I'm having this convert the number into base10 because I want the function to work both ways. Right now though, its not accepting any numbers that incorporate letters. –  Sep 16 '18 at 10:06
  • Also, I don't agree that my question is the same as that 'duplicate question'. As I said in the post, I had no idea where the issue is. Just because that turned out to be my problem doesn't mean that was what I was looking for. –  Sep 16 '18 at 10:13

0 Answers0