python – 验证基数
发布时间:2020-12-20 12:10:20 所属栏目:Python 来源:网络整理
导读:我试图创建一个程序,检查基本值的正整数,如果数字的所有数字严格小于基数,则返回True,否则返回false.我取得的一些成果是: base_and_number(12345,2) False base_and_number(12345,8) True base_and_number(9,5) False base_and_number(10110,2) True 这些肯
我试图创建一个程序,检查基本值的正整数,如果数字的所有数字严格小于基数,则返回True,否则返回false.我取得的一些成果是:
>>> base_and_number(12345,2) False >>> base_and_number(12345,8) True >>> base_and_number(9,5) False >>> base_and_number(10110,2) True 这些肯定是正确的,我已经制作了这个代码以确保它.问题是我不确定我是否以最佳方式接近它,因为我想找到一种更有效的方法来评估验证Base-n数字.这里是: def base_and_num(number,base): int_list = [] for digit in str(number): int_list.append(int(digit)) result = [] for i in range(len(int_list)): if int_list[i] < base: result.append(int_list[i]) if len(result) == len(str(number)): return True else: return False 有更简单的方法吗?我觉得有一个更明显的方法可以做到这一点,但我无法弄清楚. 解决方法
一种选择是:
def base_and_num(number,base): for i in str(number): if base <= int(i): return False return True (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |