You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
(base) PS C:\Users\Steve> python
Python 3.11.7 | packaged by Anaconda, Inc. | (main, Dec 15 2023, 18:05:47) [MSC v.1916 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> from cyaron import *
>>> dec2base
<function dec2base at 0x0000020A73598900>
>>> dec2base(123,11)
'102'
>>> dec2base(123,111)
'1C'
>>> dec2base(1234,111)
'BD'
>>> dec2base(12345,111)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Users\Steve\AppData\Roaming\Python\Python311\site-packages\cyaron\math.py", line 494, in dec2base
return dec2base(n // base, base) + convert_string[n % base]
~~~~~~~~~~~~~~^^^^^^^^^^
IndexError: string index out of range
>>>
本应出ValueError
可改为:
#source:# http://interactivepython.org/runestone/static/pythonds/Recursion/pythondsConvertinganIntegertoaStringinAnyBase.htmldefdec2base(n: int, base: int) ->str:
""" Convert a decimal number to a specified base. Args: n: The decimal number to convert. base: The base to convert the number to. Must be between 2 and 16. Returns: The number represented in the specified base. Raises: ValueError: If the base is not between 2 and 16. """# 增加下列几行if (base<2) or (base>16):
raiseValueError("base is not between 2 and 16. ")
# 结束convert_string="0123456789ABCDEF"ifn<base:
returnconvert_string[n]
returndec2base(n//base, base) +convert_string[n%base]
The text was updated successfully, but these errors were encountered:
本应出ValueError
可改为:
The text was updated successfully, but these errors were encountered: