Actually, the whole question in the title. The function itself is needed to solve the problem. The essence of the problem is that they introduce the number and bases of its number system, and it is necessary to check that the number fits the base, i.e. each digit (or lat. letter) is less than the base.

My algorithm is to convert the number to decimal, and then from the 10th it is not difficult to check, for the 10th code I have already written:

while a % 10 <= n and a != 0: a //= 10 if a == 0: print('YES') else: print('NO') 

    1 answer 1

    The standard int function throws a ValueError exception if the string cannot be converted into a number by the specified base, the first and second arguments, respectively.

    • Yes, but I need to output NO, not ValueError, if the number does not fit the base and YES if it matches. - Hashirama
    • So catch this exception. - skegg
    • Unfortunately, I do not know how, I will be glad if you explain how. Ps is just starting to learn Python. - Hashirama
    • Something like this: try: int (a, b) except ValueError: print 'NO' else: print 'YES' - skegg
    • @spirit, to write the code you need to repel each line of it with four spaces, and the code itself - with two line breaks at the beginning and end. Comments at the same time cut whitespace characters at the beginning and end of the text, so the comment should not begin with a code (you just need to put a period and two newlines, and then write the code). All this is called markdown formatting. - etki