Click here to Skip to main content
15,884,388 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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:

Python
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)+convertstring[x%base]
   return tostr(i//base,base)+convertstring[i%base]

def counter(low, high):
    current = low
    while current <= high:
        yield current
        current += 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))



Python
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)+convertstring[x%base]
   return tostr(i//base,base)+convertstring[i%base]

def counter(low, high):
    current = low
    while current <= high:
        yield current
        current += 1

for c in counter(toStr(n,base), toStr(x,base)):
    print(c)
Posted
Updated 14-Dec-18 9:47am
Comments
Patrice T 14-Dec-18 15:22pm    
what error message ?

1 solution

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))
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900