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

查看:17
本文介绍了我在基数之间转换数字的 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.

然后,它将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天全站免登陆