我在基数之间转换数字的python代码有几个错误.可能出什么问题了,我怎么找到它们? [英] My python code that converts numbers between bases has several errors. What could be wrong and how can I find them?

查看:76
本文介绍了我在基数之间转换数字的python代码有几个错误.可能出什么问题了,我怎么找到它们?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的程序是将数字从一个基数转换为另一个基数的函数.它包含三个参数:初始值,初始值的基数,然后将其转换为基数.

My program is a function that converts numbers from one base to another. It takes three arguments: the initial value, the base of the initial value, then the base it is to be converted to.

这东西有几个错误.首先,该事物将不接受任何包含cnum字母的值.我不知道为什么而且我似乎无法弄清楚如何在函数调用中强制事物将参数"cnum"识别为字符串.我必须将其转换为代码本身中的一个函数.

The thing has several errors. For one, the thing won't accept any value that contains a letter for cnum. I don't know why. And I can't seem to figure out how to force the thing to recognize the argument 'cnum' as a string within the function call. I have to convert it into a function in the code itself.

此外,我无法获得下半部分(将数字转换为最终基数的部分)的功能.要么它给了我一个无限循环(由于某种原因我无法弄清楚),要么它没有完成完整的计算.如果我输入fconbase(100,10,12),则该值应将100从10转换为12.它只吐出8.答案应该是84.

Also, I can't get the second half, the part that converts the number to the final base, to work. Either it gives me an infinite loop (for some reason I can't figure out), or it doesn't do the complete calculation. This one, if I enter fconbase(100, 10, 12) Should convert 100 from base 10 to base 12. It only spits out 8. The answer should be 84.

这是我的全部功能.

    #delcaring variables

cnum=0 #number to be converted
cbase1=0 #base the number is written in
cbase2=0 #base the number will be converted to
cnumlen=0 #number of digits
digitNum=0 #used to fetch out each digit one by one in order
exp=0 #used to calculate position in result
currentDigit="blank" #stores the digit that's been pulled from the string
result=0 #stores the result of internal calculations
decimalResult=0 #stores cnum as a base 10 number
finalResult=0 #the final result of the conversion

def fconbase(cnum, cbase1, cbase2):
    #converts number into base 10, because the math must be done in base 10
    #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 currentDigit=="A":
            currentDigit="10"
        if currentDigit=="b" or currentDigit=="B":
            currentDigit="11"
        if currentDigit=="c" or currentDigit=="C":
            currentDigit="12"
        if currentDigit=="d" or currentDigit=="D":
            currentDigit="13"
        if currentDigit=="e" or currentDigit=="E":
            currentdigit="14"
        if currentDigit=="f" or currentDigit=="F":
            currentDigit="15"
        result=int(currentDigit)
        decimalResult=decimalResult+result*(cbase1**exp)
        exp=exp+1
        digitNum=digitNum-1
    #this part converts the decimal number into the target base
    #resetting variables again
    exp=0
    result=0
    finalResult=""
    while int(decimalResult)>(cbase2**exp):
        exp=exp+1
    exp=exp-1
    while int(decimalResult)/cbase2**exp!=int(decimalResult):
        result=int(decimalResult/(cbase2**exp))
        if result==10:
            result="a"
        if result==11:
            result="b"
        if result==12:
            result="c"
        if result==13:
            result="d"
        if result==14:
            result="e"
        if result==15:
            result="f"
        finalResult=str(finalResult)+str(result)
        decimalResult=decimalResult%cbase2**exp
        exp=exp+1
    print(finalResult)

在等式的后半部分应该发生以下情况:

Here is what is supposed to happen in the latter half of the equation:

该程序解决了cbase2 ^ exp. Exp从0开始.如果该数字小于decimalResult,则它将exp(onent)增加1,然后再次尝试直到得出的数字大于decimalResult.

The program solves cbase2^exp. Exp starts at 0. If that number is less than the decimalResult, then it increases the exp(onent) by 1 and tries again until it results in a number that's greater than the decimalResult.

然后,将十进制结果除以cbase2 ^ exp.它将10到15之间的数字转换为字母(对于大于10的基数),然后将结果附加到最终结果中.应该将结果连接在一起,以形成最终的结果,然后将其打印出来.我不明白为什么它不这样做.

Then, it divides the decimalResult by cbase2^exp. It converts numbers between 10 and 15 as letters (for bases higher than 10), then appends the result to the final result. It should be concatenating the results together to form the final result that gets printed. I don't understand why its not doing that.

为什么它不能产生正确的结果,为什么不能在函数调用中输入字符串?

Why does it not generate the right result and why can't I enter a string into the function call?

推荐答案

您应该使用在第一步中找到的exp:

You should use the exp you find in the first step:

while int(decimalResult)>=(cbase2**exp):
    exp=exp+1
exp -= 1
while exp >= 0:
    ...
    finalResult=str(finalResult)+str(result)
    decimalResult=decimalResult%cbase2**exp
    exp -= 1

这篇关于我在基数之间转换数字的python代码有几个错误.可能出什么问题了,我怎么找到它们?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆