简体   繁体   English

While循环内的递归函数(Python)

[英]Recursive Function inside While loop (Python)

So I'm learning about recursion in Python and I have some code that finds the factorial of a number input by user. 因此,我正在学习Python中的递归,并且我有一些代码可以找到用户输入的数字的阶乘。

def recursion(x):
    if x == 1:
        return 1
    else:
        return(x * recursion(x - 1))

num = int(input("Enter a non-negative integer: "))
if num >= 1:
    print("The factorial of", num, "is", recursion(num))

This code works fine. 此代码可以正常工作。 That is, it will ask the question specified, allow user input, and find the factorial of the number. 也就是说,它将询问指定的问题,允许用户输入,并找到数字的阶乘。 So what I'm trying to do is to put that recursive function inside of a While loop so that after the program prints the factorial of the number submitted, it asks if the user would like to input another number. 所以我想做的是将递归函数放在While循环中,以便程序在打印提交的数字的阶乘后,询问用户是否要输入另一个数字。

loop = 1
while loop == 1:

def recursion(x):
    if x == 1:
        return 1
    else:
        return(x * recursion(x - 1))

num = int(input("Enter a non-negative integer: "))
if num >= 1:
    print("The factorial of", num, "is", recursion(num))
    print()
    answer = input(print("Would you like to find the factorial of another non-negative integer? Y/N: "))
    if answer == "Y":
        loop = 1
    else:
        loop = 0

The issue I'm having with this code is that it will ask the user for an input number, find the factorial, and ask the question "Would you like to find the factorial of another non-negative integer? Y/N:" but when I run this in IDLE the line after the above question says "None." 我在这段代码中遇到的问题是,它将要求用户输入一个输入数字,找到阶乘,然后问“是否要查找另一个非负整数的阶乘?是/否:”问题,但是当我在IDLE中运行此代码时,上述问题后的行显示“无”。

Python 3.5.0 (v3.5.0:374f501f4567, Sep 13 2015, 02:27:37) 
[MSC v.1900 64 bit (AMD64)] on win32
Type "copyright", "credits" or "license()" for more information.
Enter a non-negative integer: 6
The factorial of 6 is 720
Would you like to find the factorial of another non-negative integer? Y/N: 
NoneY
Enter a non-negative integer: 4
The factorial of 4 is 24
Would you like to find the factorial of another non-negative integer? Y/N: 
NoneN
>>> 

I looked through the Python documentation (section 4.6) and it mentions that the interpreter usually suppresses "None." 我浏览了Python文档(第4.6节) ,其中提到解释器通常禁止显示“ None”。

  1. Why, when I run this in IDLE, does it display "None" after the loop control question? 为什么当我在IDLE中运行此代码时,在循环控制问题之后它是否显示“ None”?
  2. Is there a way to omit the "None" and still use print()? 有没有办法忽略“无”而仍然使用print()?

The reason why you are seeing the None is because you have a print inside your input : 之所以看到“ None是因为您的inputprint内容:

input(print("Would you like to find the factorial of another non-negative integer? Y/N: "))

Remove that print 删除该print

input("Would you like to find the factorial of another non-negative integer? Y/N: ")

This line: 这行:

answer = input(print("Would you like to find the factorial of another non-negative integer? Y/N: "))

print returns None , so you are inputing None into your factorial function, which then gives None as an answer. print返回None ,所以您要在阶乘函数中输入None ,然后给出None作为答案。

Should be: 应该:

answer = input("Would you like to find the factorial of another non-negative integer? Y/N: ")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM