本文介绍了如何增加基本转换数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我正在尝试为4号基数制作一个计数器,但基本转换是成功的但是当它制作计数器时它会给我一个错误,有人可以帮忙吗? 我尝试过: x = 7 i = 1 n = 5 base = 4 def toStr(n,base): convertString = 0123 if n< base: return convertString [n] else : return toStr(n // base,base)+ convertString [n%base] return tostr (x // base,base)+ convertedtring [x%base] return tostr(i // base,base)+ convertedtring [i%base] def 计数器(低,高): current = low while current< = high: yield 当前当前+ = 1 for c in counter(toStr(n,base) ),toStr(x,base)): print (c) print (toStr(n,base)) print (toStr(x,base)) print (toStr(i,base)) x = 7 i = 1 n = 5 base = 4 def toStr(n,base): convertString = 0123 if n< base: return convertString [n] else : return toStr(n // base,base)+ convertString [n%base] return tostr (x // base,base)+ convertedtring [x%base] return tostr(i // base,base)+ convertedtring [i%base] def 计数器(低,高): current = low while current< = high: yield 当前当前+ = 1 for c in counter(toStr(n,base) ),toStr(x,base)): print (c) 解决方案 你试图遍历字符串而不是数字。您期望12+ 1 的结果是什么? :) 尝试: 对于柜台中的c(n,x): print( toStr(C,基峰)) I am trying make a counter for base 4 numbers the base conversion is successful but when it comes to making a counter it gives me an error, can someone help?What I have tried:x=7i=1n=5base=4def toStr(n,base): convertString = "0123" if n < base: return convertString[n] else: return toStr(n//base,base) + convertString[n%base] return tostr(x//base,base)+convertstring[x%base] return tostr(i//base,base)+convertstring[i%base]def counter(low, high): current = low while current <= high: yield current current += 1for c in counter(toStr(n,base), toStr(x,base)): print(c)print(toStr(n,base))print(toStr(x,base))print(toStr(i,base))x=7i=1n=5base=4def toStr(n,base): convertString = "0123" if n < base: return convertString[n] else: return toStr(n//base,base) + convertString[n%base] return tostr(x//base,base)+convertstring[x%base] return tostr(i//base,base)+convertstring[i%base]def counter(low, high): current = low while current <= high: yield current current += 1for c in counter(toStr(n,base), toStr(x,base)): print(c) 解决方案 You're trying to loop over strings instead of numbers. What would you expect the result of "twelvety" + 1 to be? :)Try:for c in counter(n, x): print(toStr(c,base)) 这篇关于如何增加基本转换数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 11-02 03:34