循环计数器得到阶乘

Loop counter to get factorial

我得到了以下伪代码片段

Function number_function()
Initialise variables factorial, number
Initialise Boolean valid to false
Loop while not valid
    Display “Please enter a positive integer greater than 0”
    Input number
    If number contains numeric characters
        Convert number to integer
        If number is a positive integer
            Set valid to true
        End if
    End if
End loop
Loop for counter from number to 1
    Calculate factorial = factorial x count
    If count is not 1
        Print count and format with multiply
    Else
        Print count and format with equals
    End if
End loop
Print factorial
End function

这部分是我自己写的,但是我不确定这意味着什么以及如何执行从数字到 1 的计数器循环,有人能帮忙吗?

到目前为止我的代码

def number_function():
    factorial = 0
    count = 1
    number = 0
    valid = False

    while valid != True:
        number = input("Please Enter a positive integer greater than 0 ")    
        if number.isnumeric():
            number = int(number)
            if number >= 0:
                valid = True

您必须计算阶乘并打印计算的每一步。 我是这样理解的:

def number_function():
    factorial = 0
    count = 1
    number = 0

    valid = False

    while valid != True:
        number = input("Please Enter a positive integer greater than 0 ")
        if number.isnumeric():
            number = int(number)
            if number >= 0:
                valid = True

    count = number

    factorial = 1
    while count >= 1:
        factorial = factorial * count
        if count != 1:
            print (count, "*")
        else:
            print (count, "=")
        count -= 1

    print (factorial)


number_function()

示例输出为:

Please Enter a positive integer greater than 0 3
3 *
2 *
1 =
6

你会想做这样的事情:

你不需要计数变量,因为你的数字就是你的计数器。 我在关键点添加了打印行,因此您可以了解程序的作用。 它们不是必需的,您可以在之后删除它们。

while (number > 0):       ## looping while number not reached 0
    factorial = factorial * number
    print (factorial)
    number = number - 1   ## diminish counter variable
    print (number)
print (factorial)

这将提供您的循环。在它的 header 中,它将测试条件,然后计算你的阶乘方程,然后减少你的数字以再次相乘,但变小直到它达到零。

我不是很明白,rest是什么意思,可能是我英语水平不行。 ;)