“int”对象没有“扩展”的属性——Python。

时间:2022-04-20 08:49:01

I am trying to evaluate power series using python. series => e^x = 1+ x+ x^2/2! + x^3/3!...x^n/n! I am getting this error ''int' object has no attribute 'extend'.

我正在尝试用python来评估幂级数。系列=> e x = 1+ x+ x 2/2!+ x ^ 3/3 !…x ^ n / n !我得到这个错误“int”对象没有属性“extend”。

My code:

我的代码:

import math
print('give x')

x = float(input())

n =100
k = 0
list = (1)

while 0<x<1:
      list.extend([math.pow(x,K+1))])
      k = k+1
      if x==n:
            break

print(sum(list))

Please help!

请帮助!

2 个解决方案

#1


1  

There are multiple problems with your code.

您的代码有很多问题。

Firstly, you are attempting to create a list with (1) - that just creates the integer object 1, the parentheses have no effect here. To create a list containing 1 you need [1]. And you shouldn't use the names of Python built-ins (like list) as variable names - not only is it confusing to other people who may read your code it makes the built-in inaccessible, which can lead to mysterious bugs.

首先,您试图创建一个包含(1)的列表,它只创建整数对象1,括号在这里没有效果。要创建包含1的列表,需要[1]。而且,您不应该使用Python内置的名称(如列表)作为变量名称——这不仅会使其他可能阅读您的代码的人感到困惑,因为它会使内置的不可访问,从而导致神秘的bug。

K is not the same as k.

K不等于K。

Your while 0<x<1: test does't make much sense; FWIW, the Taylor series for ex converges for all values of x.
Your if x==n: test should be if k==n:, although it'd be better to use a for loop with range (or maybe xrange in Python 2).

0

You don't need to save the terms in a list - just add them as you go.

你不需要把术语保存在列表中——只要添加它们就行了。

You don't need math.pow - x**k calculates the same thing as math.pow(x, k), but even that's unnecessary here: you should just keep track of the previous term and multiply it by x on each loop.

你不需要数学。pow - x**k计算和数学一样的东西。pow(x, k),但即使这样也没有必要:你应该跟踪前一项,并在每个循环上乘以x。

You forgot the /n!. Once again, you don't really need to compute the factorial (or call the math.factorial function) since you can just divide the previous term by k.

你忘记了/ n !。再一次,你不需要计算阶乘(或者叫数学)。因为你可以把前一项除以k。

Hopefully, that's given you enough clues to fix your code. I won't provide working code at this stage, since I suspect this is a homework problem. Note that the math module has an exp function which you can use to test the accuracy of your power series calculations.

希望这给了您足够的线索来修复代码。我不会在这个阶段提供工作代码,因为我怀疑这是一个作业问题。注意,math模块有一个exp函数,可以用来测试power系列计算的准确性。

#2


0  

A list literal is created with square brackets, []. You can use parentheses, (), for grouping or for creating a tuple. In the case of list = (1), they are being used for grouping, so this is the same as list = 1. (A tuple with one element is created with mytuple = (1,) or just mytuple = 1,.)

列表文字是用方括号来创建的[]。可以使用括号()、分组或创建元组。在list =(1)的情况下,它们被用于分组,所以这和list = 1是一样的。(使用mytuple =(1,)或仅mytuple = 1创建一个元组。)

At this point, I'll mention that naming one of your variables list masks the built-in function list, so after you do that you can't access that function anymore without some effort. It's best to name your object something else, like lst.

在这一点上,我要提到的是,命名一个变量列表会掩盖内置的函数列表,所以在您完成之后,您就不能再使用这个函数了。最好将对象命名为其他东西,比如lst。

A list's extend() method adds all the elements from the passed list onto the object you're accessing, so if mylist was [1, 2, 3], mylist.extend([4, 5]) would result in mylist becoming [1, 2, 3, 4, 5]. However, you only have one object to add, so it makes more sense to use append(), which adds the passed object to the given list.

列表的extend()方法将传递的列表中的所有元素添加到正在访问的对象上,所以如果mylist是[1、2、3],mylist。扩展([4,5])将导致mylist成为[1、2、3、4、5]。但是,您只需要添加一个对象,因此使用append()更有意义,它将传递的对象添加到给定的列表中。

x = float(input('Give x: ')) # the input function can be passed a prompt string
n = 100
k = 0
lst = [1] # changed name, created a list

while 0<x<1:
      list.append(x**(k+1)) # you can just use the ** operator if you want
                            # also, k isn't K
      k = k+1
      if x==n: # x is never changed, so your loop either never runs
               # or goes forever
            break

print(sum(lst))

Note the while loop that will either never be entered or never finish. You'll have to take another look at your program's logic.

注意,while循环要么永远不会进入,要么永远不会完成。你得再看看你的程序的逻辑。

#1


1  

There are multiple problems with your code.

您的代码有很多问题。

Firstly, you are attempting to create a list with (1) - that just creates the integer object 1, the parentheses have no effect here. To create a list containing 1 you need [1]. And you shouldn't use the names of Python built-ins (like list) as variable names - not only is it confusing to other people who may read your code it makes the built-in inaccessible, which can lead to mysterious bugs.

首先,您试图创建一个包含(1)的列表,它只创建整数对象1,括号在这里没有效果。要创建包含1的列表,需要[1]。而且,您不应该使用Python内置的名称(如列表)作为变量名称——这不仅会使其他可能阅读您的代码的人感到困惑,因为它会使内置的不可访问,从而导致神秘的bug。

K is not the same as k.

K不等于K。

Your while 0<x<1: test does't make much sense; FWIW, the Taylor series for ex converges for all values of x.
Your if x==n: test should be if k==n:, although it'd be better to use a for loop with range (or maybe xrange in Python 2).

0

You don't need to save the terms in a list - just add them as you go.

你不需要把术语保存在列表中——只要添加它们就行了。

You don't need math.pow - x**k calculates the same thing as math.pow(x, k), but even that's unnecessary here: you should just keep track of the previous term and multiply it by x on each loop.

你不需要数学。pow - x**k计算和数学一样的东西。pow(x, k),但即使这样也没有必要:你应该跟踪前一项,并在每个循环上乘以x。

You forgot the /n!. Once again, you don't really need to compute the factorial (or call the math.factorial function) since you can just divide the previous term by k.

你忘记了/ n !。再一次,你不需要计算阶乘(或者叫数学)。因为你可以把前一项除以k。

Hopefully, that's given you enough clues to fix your code. I won't provide working code at this stage, since I suspect this is a homework problem. Note that the math module has an exp function which you can use to test the accuracy of your power series calculations.

希望这给了您足够的线索来修复代码。我不会在这个阶段提供工作代码,因为我怀疑这是一个作业问题。注意,math模块有一个exp函数,可以用来测试power系列计算的准确性。

#2


0  

A list literal is created with square brackets, []. You can use parentheses, (), for grouping or for creating a tuple. In the case of list = (1), they are being used for grouping, so this is the same as list = 1. (A tuple with one element is created with mytuple = (1,) or just mytuple = 1,.)

列表文字是用方括号来创建的[]。可以使用括号()、分组或创建元组。在list =(1)的情况下,它们被用于分组,所以这和list = 1是一样的。(使用mytuple =(1,)或仅mytuple = 1创建一个元组。)

At this point, I'll mention that naming one of your variables list masks the built-in function list, so after you do that you can't access that function anymore without some effort. It's best to name your object something else, like lst.

在这一点上,我要提到的是,命名一个变量列表会掩盖内置的函数列表,所以在您完成之后,您就不能再使用这个函数了。最好将对象命名为其他东西,比如lst。

A list's extend() method adds all the elements from the passed list onto the object you're accessing, so if mylist was [1, 2, 3], mylist.extend([4, 5]) would result in mylist becoming [1, 2, 3, 4, 5]. However, you only have one object to add, so it makes more sense to use append(), which adds the passed object to the given list.

列表的extend()方法将传递的列表中的所有元素添加到正在访问的对象上,所以如果mylist是[1、2、3],mylist。扩展([4,5])将导致mylist成为[1、2、3、4、5]。但是,您只需要添加一个对象,因此使用append()更有意义,它将传递的对象添加到给定的列表中。

x = float(input('Give x: ')) # the input function can be passed a prompt string
n = 100
k = 0
lst = [1] # changed name, created a list

while 0<x<1:
      list.append(x**(k+1)) # you can just use the ** operator if you want
                            # also, k isn't K
      k = k+1
      if x==n: # x is never changed, so your loop either never runs
               # or goes forever
            break

print(sum(lst))

Note the while loop that will either never be entered or never finish. You'll have to take another look at your program's logic.

注意,while循环要么永远不会进入,要么永远不会完成。你得再看看你的程序的逻辑。