Python,将级数和与阶乘相加

Python, add sum of series with factorial

我需要为以下问题编写 python 代码:

-编写函数要求用户输入正整数 n 和整数 x 并显示级数的总和:

1+1⁄1!+2⁄2!+3⁄3!+⋯+⁄!

要求:

  1. 输入验证。
  2. 编写一个函数来计算数字的阶乘。
  3. 写一个函数来计算级数的总和。
  4. 不允许导入库。

所以基本上,它需要向用户询问正整数 (n) 和正整数 (x) 来解决 1+1⁄1!+2⁄2!+3⁄3!+⋯+⁄!然后显示总和。我认为应该是两个方程,没有数学库。

这是我目前的代码:

n = int(input('Enter a positive integer: '))
x = int(input('Enter another positive integer: '))
#  n >= 0
fact(x)

我不知道如何继续用户输入,我应该使用 for 循环还是?

n = int(input('Enter a positive integer: '))
# x = int(input('Enter another positive integer: '))
answer = 1
for i in range (n,0,-1):
    result = answer*i
print(n, answer)

所以,我想出了如何计算阶乘,但现在我如何计算等式的其余部分?

看来你要的是:

def fact(k): # k!
    out = 1
    for i in range(1, k+1):
        out *= i
    return out

result = 1
for i in range(1, n+1):
    result += x**i / fact(i)

Write functions to ask the user for a positive integer n and and integer x and ...

您需要的是一个函数,因此,对于第一步,我强烈建议您避免使用 print 函数。看来,除了你提到的问题,还有一些逻辑错误。由于问题需要你编写函数,所以我打算尽可能使用这些函数来解决它。我想出的是以下内容:

# 4. No importing was intended

# 2. Write a function to calculate factorials for a number.
def fact(n):
  mult = 1
  for i in range(1, n+1):
    mult *= i
  return mult

# 3. Write a function to calculate factorials for a number.
def sum_series(s):
  temp = 0
  for value in s:
    temp += value
  return temp

def main():
  n = input('Enter a positive integer: ')
  x = input('Enter another positive integer: ')

  # 1. Input validation
  if not n.isnumeric():
    return False
  if not x.isnumeric():
    return False
  n = int(n)
  x = int(x)

  tempSeries = [1]
  for i in range (1,n+1):
      tempSeries.append((x**i)/fact(i))
  return sum_series(tempSeries)

例子

因此,如果我们将 2 作为 n,将 3 作为 x,我们将得到如下结果:

1 + (3^1)/1 + (3^2)/2 = 8.5

让我们检查一下函数:

main()

输出

Enter a positive integer: 2
Enter another positive integer: 3
8.5

I am stuck on how to continue past the user input, should i use a for loop or?

您需要验证用户的输入并要求编写正确的输入,因此您可以使用此处的函数:

def ask_user(): # Define function that asks to type variables
    n = input('Enter a positive integer for n: ')
    
    while True:
        if n[0] == "-" and n[1::].isnumeric(): 
        # If the variable is not positive number, ask to type positive
            n = input("Please, provide positive integer: ")
        elif not n.isnumeric():
        # If the variable is not number at all
            n = input("Please, provide number for n: ")
        else:
            break
    
    x = input('Enter a positive integer for x: ')
    
    while True:
        if x[0] == "-" and x[1::].isnumeric():
            x = input("Please, provide positive integer: ")
        elif not x.isnumeric():
            x = input("Please, provide number for x: ")
        else:
            break
    

    return int(n), int(x)

n, x = ask_user()
print(n)
print(x)

它给出下一个输出:

Enter a positive integer for n: haha
Please, provide number for n: -5
Please, provide positive integer: 5
Enter a positive integer for x: blabla
Please, provide number for x: -99
Please, provide positive integer: 2
5
2

关于方程部分

您需要阶乘函数:

def factorial(n):
  result = 1
  for i in range(1, n+1):
    result *= i
  return result

(X!^N)/N

我需要更多关于它的数据,但如果你的方程是下一个:

(X!^N)/N

然后你可以使用Amirhossein Kiani提到的这里的功能,但稍微重新设计一下:

tempSeries = [1]
  for i in range (1,n+1):
      tempSeries.append((factorial(x)**i)/i)
  return sum_series(tempSeries)

(X^(N)/N)!

tempSeries = [1]
  for i in range (1,n+1):
      tempSeries.append(factorial((x**i)/i))
  return sum_series(tempSeries)

(X^N)!/N

tempSeries = [1]
  for i in range (1,n+1):
      tempSeries.append(factorial(x**i)/i)
  return sum_series(tempSeries)

等等...