Python 如何在while循环中返回值
我正在为我的一门CS课程做作业。我已经了解了这一点,但我不知道如何在while循环中返回值 我遇到的问题是,我需要在while循环中每次添加商,直到t==0。在除法运算之前,所有的运算都是正确的,所有的运算都是将相同的两个数相加。我需要它做的是记住“除法”在循环中等于前一项,然后将其添加到当前循环计算的值中 我希望这有点道理。Python 如何在while循环中返回值,python,while-loop,return-value,Python,While Loop,Return Value,我正在为我的一门CS课程做作业。我已经了解了这一点,但我不知道如何在while循环中返回值 我遇到的问题是,我需要在while循环中每次添加商,直到t==0。在除法运算之前,所有的运算都是正确的,所有的运算都是将相同的两个数相加。我需要它做的是记住“除法”在循环中等于前一项,然后将其添加到当前循环计算的值中 我希望这有点道理。 #公式如下 #1+x+(x^t)/(t!)直到t==1 t=int(输入(“输入t:”的非负整数) x=浮点(输入(“为x输入实数:”) 事实=1 最终产品=1 计数器
#公式如下
#1+x+(x^t)/(t!)直到t==1
t=int(输入(“输入t:”的非负整数)
x=浮点(输入(“为x输入实数:”)
事实=1
最终产品=1
计数器=1
而计数器则应用于将变量传递给下一个循环
def factorial(n):
if n == 1:
return 1
else:
return n * factorial(n-1)
它正在将“n-1”值传递给下一个循环。如果要将core的返回值分配回本地y变量,则不通过引用传递:
def factorial(n):
if n == 1:
return 1
else:
return n * factorial(n-1)
y=芯(x)
在进入循环之前,还需要设置y。函数中的局部变量在其他函数中不可用
因此,您根本不需要将y传递给核心(x):
def核心(x):
y=输入(“选择一个数字:”)
如果y==x:
打印(“您输入了正确的号码!”)
返回y
如果y>x:
打印(“数字较低,请重试”)
返回y
其他:
打印(“数字更高,请重试”)
返回y
循环变成:
y=无
而(x!=y)和(i
在main()函数中设置y的起始值没有多大关系,只要在用户猜测之前y永远不等于x。这与指导老师给出的问题描述非常接近:
x = float(input("Enter a real number for x: "))
t = int(input("Enter a non negative integer for t: "))
counter = 1
series = 1
num = 1
denom = 1
while counter <= t :
num = num * x
denom = denom * counter
series = series + num / denom
counter = counter + 1
print(series)
这是一个稍微扩展的版本
首先,您应该意识到给定的序列是e**x
的近似值;包含的术语越多,最终结果越准确。让我们探讨一下:
import math
def approx_ex(x, max_t):
"""
Maclaurin series expansion for e**x
"""
num = 1 # == x**0
denom = 1 # == 0!
total = 1.0 # term_0 == (x**0) / 0!
for t in range(1, max_t + 1):
# modify numerator and denominator to find next term
num *= x # x**(t-1) * x == x**t
denom *= t # (t-1)! * t == t!
# keep a running total
total += num / denom
return total
def main():
x = float(input("Input a real number: "))
actual = math.e ** x
print("\nApproximation of e ** {} == {}\n".format(x, actual))
for terms in range(1, 16):
approx = approx_ex(x, terms)
error = approx - actual
print("{:>2d}: {:16.12f} ({:16.12f})".format(terms, approx, error))
if __name__ == "__main__":
main()
这就像
Input a real number: 3.205
Approximation of e ** 3.205 == 24.655500016456244
1: 4.205000000000 (-20.450500016456)
2: 9.341012500000 (-15.314487516456)
3: 14.827985854167 ( -9.827514162290)
4: 19.224423254193 ( -5.431076762264)
5: 22.042539627609 ( -2.612960388847)
6: 23.547883457076 ( -1.107616559380)
7: 24.237115881853 ( -0.418384134603)
8: 24.513239622030 ( -0.142260394426)
9: 24.611570353948 ( -0.043929662508)
10: 24.643085353528 ( -0.012414662928)
11: 24.652267678406 ( -0.003232338051)
12: 24.654720124342 ( -0.000779892115)
13: 24.655324746590 ( -0.000175269866)
14: 24.655463161897 ( -0.000036854559)
15: 24.655492736635 ( -0.000007279822)
这非常清楚地表明,随着更多术语的汇总,结果是如何变得越来越好的。不仅要感谢您在这个问题上的帮助,还要感谢那个伟大的网站。嗯。。。虽然存在非常适合递归的问题,但这不是其中之一!迭代解会更快,占用更少的内存。@HughBothwell我知道有一个函数可以用来找到阶乘,但我们在课堂上一直在研究while循环,所以我想他是在找我们在这个作业中使用while循环,因为我们的大多数学生都不知道如何调用单独的函数。@MehmetMertYidiran我不明白我到底应该如何处理这段代码。我的程序中已经有了一个简单的设置,就是找到阶乘并将其用作“x^t”的分母,我只是不知道如何继续添加后续的答案“(x^t)/直到循环结束。其他一切都很好。这到底是什么?哇,我真不敢相信我已经花了几个小时试图弄明白这一点,这就是我要做的。非常感谢。
import math
def approx_ex(x, max_t):
"""
Maclaurin series expansion for e**x
"""
num = 1 # == x**0
denom = 1 # == 0!
total = 1.0 # term_0 == (x**0) / 0!
for t in range(1, max_t + 1):
# modify numerator and denominator to find next term
num *= x # x**(t-1) * x == x**t
denom *= t # (t-1)! * t == t!
# keep a running total
total += num / denom
return total
def main():
x = float(input("Input a real number: "))
actual = math.e ** x
print("\nApproximation of e ** {} == {}\n".format(x, actual))
for terms in range(1, 16):
approx = approx_ex(x, terms)
error = approx - actual
print("{:>2d}: {:16.12f} ({:16.12f})".format(terms, approx, error))
if __name__ == "__main__":
main()
Input a real number: 3.205
Approximation of e ** 3.205 == 24.655500016456244
1: 4.205000000000 (-20.450500016456)
2: 9.341012500000 (-15.314487516456)
3: 14.827985854167 ( -9.827514162290)
4: 19.224423254193 ( -5.431076762264)
5: 22.042539627609 ( -2.612960388847)
6: 23.547883457076 ( -1.107616559380)
7: 24.237115881853 ( -0.418384134603)
8: 24.513239622030 ( -0.142260394426)
9: 24.611570353948 ( -0.043929662508)
10: 24.643085353528 ( -0.012414662928)
11: 24.652267678406 ( -0.003232338051)
12: 24.654720124342 ( -0.000779892115)
13: 24.655324746590 ( -0.000175269866)
14: 24.655463161897 ( -0.000036854559)
15: 24.655492736635 ( -0.000007279822)