You return four variables s1,s2,s3,s4 and receive them using a single variable obj. This is what is called a tuple, obj is associated with 4 values, the values of s1,s2,s3,s4. So, use index as you use in a list to get the value you want, in order.
Copyobj=list_benefits()
print obj[0] + " is a benefit of functions!"
print obj[1] + " is a benefit of functions!"
print obj[2] + " is a benefit of functions!"
print obj[3] + " is a benefit of functions!"
Answer from Aswin Murugesh on Stack OverflowAttributeError: 'tuple' object has no attribute '' Error Help
deep learning - how can I convert tensor shape to numpy array? - Data Science Stack Exchange
AttributeError: 'tuple' object has no attribute 'detach'
Getting AttributeError: 'tuple' object has no attribute 'items'
Videos
You return four variables s1,s2,s3,s4 and receive them using a single variable obj. This is what is called a tuple, obj is associated with 4 values, the values of s1,s2,s3,s4. So, use index as you use in a list to get the value you want, in order.
Copyobj=list_benefits()
print obj[0] + " is a benefit of functions!"
print obj[1] + " is a benefit of functions!"
print obj[2] + " is a benefit of functions!"
print obj[3] + " is a benefit of functions!"
You're returning a tuple. Index it.
Copyobj=list_benefits()
print obj[0] + " is a benefit of functions!"
print obj[1] + " is a benefit of functions!"
print obj[2] + " is a benefit of functions!"
I am trying to make a discord bot that would turn a message into lowercase. I am encountering an error, as the title suggests, "AttributeError: 'tuple' object has no attribute 'lower'. "
Here is my code if anyone can help.
https://hastebin.com/ibareyilax.py
If this is the wrong subreddit I don't mind taking down my post and posting it elsewhere.
from kubernetes import client, config
config.load_kube_config()
v1 = client.CoreV1Api()
print("Listing services with their IPs:")
ret = v1.list_service_for_all_namespaces_with_http_info(watch=False)
for i in ret.items:
print("%s\t%s\t%s" % (i.status.pod_ip, i.metadata.namespace, i.metadata.name)) This throws this error: File "filename.py", line 8, in <module> for i in ret.items: AttributeError: 'tuple' object has no attribute 'items'
But when I simply print(ret), it certainly LOOKS like a tuple, which means I should be able to iterate through with tuple.items, no?
In the line:
Jobs = ()
you create a tuple. A tuple is immutable and has no methods to add, remove or alter elements. You probably wanted to create a list (lists have an .append-method). To create a list use the square brackets instead of round ones:
Jobs = []
or use the list-"constructor":
Jobs = list()
However some suggestions for your code:
opening a file requires that you close it again. Otherwise Python will keep the file handle as long as it is running. To make it easier there is a context manager for this:
with open('Jobs.txt') as openFile:
x = 1
while x != 0:
Stuff = openFile.readline(x)
if Stuff != '':
Jobs.append(Stuff)
else:
x = 0
As soon as the context manager finishes the file will be closed automatically, even if an exception occurs.
It's used very rarely but iter accepts two arguments. If you give it two arguments, then it will call the first each iteration and stop as soon as the second argument is encountered. That seems like a perfect fit here:
with open('Jobs.txt') as openFile:
for Stuff in iter(openFile.readline, ''):
Jobs.append(Stuff)
I'm not sure if that's actually working like expected because openFile.readline keeps trailing newline characters (\n) so if you want to stop at the first empty line you need for Stuff in iter(openFile.readline, '\n'). (Could also be a windows thingy on my computer, ignore this if you don't have problems!)
This can also be done in two lines, without creating the Jobs before you start the loop:
with open('Jobs.txt') as openFile:
# you could also use "tuple" instead of "list" here.
Jobs = list(iter(openFile.readline, ''))
Besides iter with two arguments you could also use itertools.takewhile:
import itertools
with open('Jobs.txt') as openFile:
Jobs = list(itertools.takewhile(lambda x: x != '', openFile))
The lambda is a bit slow, if you need it faster you could also use ''.__ne__ or bool (the latter one works because an empty string is considered False):
import itertools
with open('Jobs.txt') as openFile:
Jobs = list(itertools.takewhile(''.__ne__, openFile))
The Jobs object you created is a tuple, which is immutable. Therefore, you cannot "append" anything to it.
Try
Jobs = []
instead, in which you create a list object.
Hello!
i have created a list in my code that ends in me having a list of coordinates listed as such:
0: (x,y)
1: (x,y)
2: (x,y) …..
that coordinates belong to little squares that have corresponding colours.
i wanted to make it:
0: (x,y,’RED’)
1: (x,y,’BLUE’)
2: (x,y,’BLUE’) …..
but when i call my list and do list[0].append(‘RED’) i get ‘tuple’ object has no attribute ‘append’
i wanted to do it this way because there can be a lot of boxes and i’d like to do it inside a for loop
any help??