python-3.x - 返回 Null 类型数据的 Python 函数
问题描述
我正在尝试计算给定值的 'nth Fibonacci % m '。(使用 pisano 的系列)。这是终端正在显示的消息。
错误信息->
100 2
Traceback (most recent call last):
File "fibag.py", line 27, in <module>
print(huge_fibo(n,m))
File "fibag.py", line 21, in huge_fibo
return get_fibo(rem) % m
TypeError: unsupported operand type(s) for %: 'NoneType' and 'int'
#uses python3
def pisano_len(m):
prev=0
next=1
for i in range(m*m+1):
prev,next=next,(prev+next)%m
if(prev==0 and next==1):
return i+1
def get_fibo(n):
if(n<1):
return n
prev=0
curr=1
for i in range(n-1):
(prev,curr)=(curr,prev+curr)
return curr
def huge_fibo(n,m):
rem=int(n%pisano_len(m))
return get_fibo(rem) % m
if(__name__=='__main__'):
n,m=map(int,input().split())
print(huge_fibo(n,m))
想不通原因。
解决方案
您的get_fibo()
函数不执行 return 语句 if n == 1
,有效地返回None
。
推荐阅读
- java - 是否有任何库可以让我们在 recyclerview 中获得选取框效果(例如 hotstar vip 页面滚动图像)?
- c# - 如何为具有不同属性的嵌套类定义映射?
- angular - ngx-chips 自动完成返回值以及显示字符串
- html - 动态加载html
- android - 当我尝试从 firebase 检索图像时出现错误提示“你声明了这个活动”
- php - laravel 项目中的作曲家冲突
- java - 如何在回收站视图中过滤数据
- php - 使用 PHP 来“解析”带有响应头“content-type: image/jpeg”的图像
- android - 如何将数组放入复选框并根据数组 id react native 点击它?
- c# - 是否可以在 SonarQube 中添加单元测试项目的覆盖率报告和代码分析?如果是这样,我应该如何在 SonarQube 中配置它们?