这是我的代码,我只是不明白这个代码不工作。你知道吗
def theBase(n, b):
convertString = "0123456789"
if n < b:
return convertString[n]
else:
return toBase(n//b,b) + convertString[n%b]
def toBase(n, b):
print(theBase(n, b), end="")
def main():
n = int(input())
b = int(input())
print(n, "in base", b, "is ", end="")
toBase(n, b)
if __name__ == "__main__":
main()
不修改主功能
出现此问题的原因是函数
toBase()
返回None
,因为没有显式的返回语句。然后函数theBase()
中的最后一行尝试将来自toBase()
(无)的返回值添加到字符串中,这将给出您看到的异常。你知道吗我认为您有一个简单的错误,
theBase()
应该调用自身,而不是toBase()
(请参阅该函数的最后一行)。以下操作将解决此问题:相关问题 更多 >
编程相关推荐