So I've defined a recursive function numToBaseB that converts a given number in base ten into any other base between 2 and 10. The desired output is a string, but for some reason I keep getting an int.
def numToBaseB(num, b):
if num == 0:
return ''
elif b > 10 or b < 2:
return "The base has to be between 2 and 10"
else:
return numToBaseB(num // b, b ) + str(num % b)
So for me: numToBaseB(4, 2)
would return
100
instead of the desired output:
'100'
Your program is working as designed:
>>> numToBaseB(1024,2)
'10000000000'
>>> numToBaseB(4,2)
'100'
Of course, if you do print(numToBaseB(4,2))
, the quotes will not be displayed.
If you want to have quotes though, you can always do this:
print (" ' "+numToBase(4,2)+" ' ")
But in the program, if you use str() anyways, it will be treated as string ofcourse.
:)
Edit: typing on phone, so sorry for this madness :(
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.