【问题标题】:Why is there a exta 'NONE' output when i enter a negative integer.I only expect 'Plese Enter A Positive Integer 'output为什么当我输入负整数时会有额外的“BONE”输出。我只期望“请输入正整数”输出
【发布时间】:2021-10-02 05:00:53
【问题描述】:
def fact(n):
if n<0:
    print('Plese Enter A Positive Integer ')
elif n==0:
    return 1

else:
 return n*fact(n-1)

print(fact(int(input('Plaese Enter The Integer That You Want To Find The Facorial Value'))))

为什么当我输入一个负整数时会有一个额外的“NONE”输出。我只希望当我输入一个负数时输出“请输入一个正整数”

【问题讨论】:

    标签: python-3.x recursion


    【解决方案1】:

    它的 bcos 你说的是打印 fact(n) 的返回值。所以当 n 为负时没有返回。所以它打印的返回值是none。要解决这个问题,我们可以使用

     if n<0:
      return 'Please Enter A Positive Integer '
    

    【讨论】:

      猜你喜欢
      • 2017-07-28
      • 2016-01-29
      • 2023-02-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-09-21
      • 2016-12-06
      相关资源
      最近更新 更多