[英]How would i write a function in python using variables for a sum of exponential series?
I am trying to create a code in which you input a starting integer, number of terms, and the exponent. 我正在尝试创建一个代码,在其中您输入一个起始整数,项数和指数。 This is my current code, i have run it with just numbers, but cannot figure out how to run it with variables, that are prompted by the user. 这是我当前的代码,我只用数字运行它,但无法弄清楚如何用用户提示的变量运行它。
def squares(s,n,e):
for x in range(s,s+n):
Sum=0
Sum=sum(Sum+x**e)
s=int(input("input starting integer:"))
n=int(input("input number of terms:"))
e=int(input("input exponent:"))
print(squares(s,n,e))
Fixing some of the syntax (eg sum() takes an iterable, not a constant), I believe your code works: 修复了某些语法(例如sum()需要一个可迭代的,而不是常量的),我相信您的代码可以工作:
def squares(s,n,e ):
for x in range(s,s+n):
Sum=0
Sum+=sum([Sum, x**e])
return Sum
s=int(input("input starting integer:")) #e.g. '9'
n=int(input("input number of terms:")) #e.g. '4'
e=int(input("input exponent:")) #e.g. '3'
print(squares(s,n,e)) # result: 1728
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.