+ 1
List operations
#this is my code def TenToOne(n): n=[0, 1, 2, 3, 4, 5, 6, 7, 8, 9] TenToOne.reverse() #i got this message at python shell >>> TenToOne.reverse() Traceback (most recent call last): File "<pyshell#3>", line 1, in <module> TenToOne.reverse() AttributeError: 'function' object has no attribute 'reverse' >>> anybody can help me hwo to fix that?
7 ответов
+ 2
Your error is because your attempting to reverse your function.. What you want to do is reverse your list INSIDE your function
-----------------------------
def TenToOne(n):
n.reverse()
return n
TenToOne([1,2,3,4,5,6,7,8,9])
-----------------------------
Let's Break It down..
line #1 - Define our function and set a variable "n" to hold our list when we call the function
line #2 - Reverse our list which we assigned to n
line #3 - Return n as our reversed list
line #4 - Call the function and assign our list as variable n
+ 1
>>> TenToOne()
Traceback (most recent call last):
File "<pyshell#0>", line 1, in <module>
TenToOne()
TypeError: TenToOne() missing 1 required positional argument: 'n'
>>> TenToOne(n)
Traceback (most recent call last):
File "<pyshell#1>", line 1, in <module>
TenToOne(n)
NameError: name 'n' is not defined
>>> myfunction.TenToOne
Traceback (most recent call last):
File "<pyshell#2>", line 1, in <module>
myfunction.TenToOne
NameError: name 'myfunction' is not defined
>>>
+ 1
I dont know if I made it correct, but I cant run the program
+ 1
if your using the function I wrote, you use it like this
TenToOne([1,2,3,4,5,6,7,8,9])
if you want to print that or store it to a variable you can do so like this
backward=TenToOne([1,2,3,4,5,6,7,8,9])
print(backward)
or directly print like this
print(TenToOne([1,2,3,4,5,6,7,8,9]))
+ 1
THIS IS HOW YOU WRITE THE FUNCTION
def TenToOne(n):
n.reverse()
return n
THIS IS HOW YOU CALL YOUR FUNCTION
TenToOne([1,2,3,4,5,6,7,8,9])
+ 1
yhe, done :)
thanks a lot
0
does this help?