我的程序是一个将数字从一个基数转换为另一个基数的函数。它需要三个参数:初始值,初始值的基数,然后是要转换为的基数。
这件事有几个错误。首先,该东西不会接受包含cnum字母的任何值。我不知道为什么。我似乎无法弄清楚如何强制事物将'cnum'参数识别为函数调用中的字符串。我必须将它转换为代码本身的函数。
此外,我无法得到下半部分,即将数字转换为最终基数的部分。要么它给了我一个无限循环(由于某种原因我无法弄清楚),或者它没有完成整个计算。这个,如果我输入fconbase(100,10,12)应该将100从基数10转换为基数12.它只吐出8.答案应该是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)
以下是应该在后半部分中发生的事情:
该程序解决了cbase2 ^ exp。 Exp从0开始。如果该数字小于decimalResult,则它将exp(onent)增加1并再次尝试,直到它产生的数字大于decimalResult。
然后,它将decimalResult除以cbase2 ^ exp。它将10到15之间的数字转换为字母(对于大于10的基数),然后将结果附加到最终结果。它应该将结果连接在一起以形成打印的最终结果。我不明白为什么不这样做。
为什么它不能生成正确的结果,为什么我不能在函数调用中输入字符串?
您应该使用在第一步中找到的exp:
while int(decimalResult)>=(cbase2**exp):
exp=exp+1
exp -= 1
while exp >= 0:
...
finalResult=str(finalResult)+str(result)
decimalResult=decimalResult%cbase2**exp
exp -= 1
如果没有涉及代码的具体问题,正如您所说的那样,我将简要回答标题中的实际问题。
可能出现什么问题,如何找到[我的代码中的错误]?
而不是将你的代码视为一个大的复杂函数,你必须同时盯着并理解所有(我很少能同时在我自己的内部大脑缓存中保存超过10行代码),尝试将其分解为更小的部分“首先我这样做,并期待这个结果。然后我把结果做到这一点,并期待另一个结果。”
从您对问题的描述来看,您似乎已经在考虑这种方式了,但您仍然倾倒了大量代码,似乎很难弄清楚问题究竟在哪里。许多初学者会写一些大量的代码,然后在测试时将其视为黑盒子。就像“我没有得到正确的答案,我不知道问题从哪里开始。”这是学习良好的调试技巧至关重要的地方。
我会首先将事情分解成更小的部分,以便在交互式Python提示下尝试。为不同的变量输入虚拟值,并确保代码的小片段(1到5行左右,小到足以让它易于推理)完全按照您对变量的不同值所做的操作。
如果这没有帮助,那么对于初学者来说,通常对于初学者和高级开发人员而言,经过尝试的真正方法是使用print语句来篡改代码。在您认为必要的许多地方,放置一个语句来打印一个或多个变量的值。像print("exp = %s; result = %s" % (exp, result)
。将此内容放在您需要的多个位置,以通过执行来跟踪某些变量的值。看看它开始提供没有意义的答案。
有时这很难做到。您可能无法猜出放置print
语句的最有效位置,甚至是打印的重要内容。在这种情况下(在大多数情况下都是IMO),使用像pdb
中内置的Python这样的交互式调试器会更有效。有很多很好的资源可以学习pdb
,但基础知识不应该花太长时间才能下来并且会给你带来很多麻烦。
pdb
将逐行运行您的代码,在每一行之后停止(并在循环中逐步遍历循环中的每个循环),允许您在前进到下一行之前检查每个变量的内容。这使您有充分的能力来检查代码的每个部分是否符合您的预期,并且可以帮助您查明许多问题区域。
首先,不需要代码的整个第一部分,因为int函数为您完成。而不是所有这些,你可以做到这一点。
int(cnum, base=cbase1)
这会将cnum从cbase1转换为10。
第二部分可能会进入无限循环,因为在底部,它说
exp = exp + 1
什么时候应该说
exp = exp - 1
因为你想从(例如)5 ^ 2到5 ^ 0。
结果没有最后一位数是因为它在exp = 0时突破了循环。它实际上并没有将数字添加到结果中。一个简单的解决方法是
finalResult = str(finalResult) + str(decimalResult)