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 OverflowYou 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!"
AttributeError: 'tuple' object has no attribute 'join' - Post.Byes
python - AttributeError: 'tuple' object has no attribute 'append' - Stack Overflow
AttributeError: 'tuple' object has no attribute 'enter'
ERROR: AttributeError: 'tuple' object has no attribute 'to'
Videos
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.
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?
You have an array of one-element tuples (intentionally or not). The first and the only element of each tuple is a numpy array. Extend that array:
for i in range(len(a)):
a[i][0].join(a[0])
If, however, you want to add the second element to the tuples, you have to convert a0 to a tuple:
a0 = (a0,)
for i in range(len(a)):
a[i] = a[i] + a0
In your current code, a0 is not a tuple but a numpy array, you need to add a comma to indicate you want it to be a tuple when you have only one item i.e.:
a0 = (np.array([0.33333332, 0.66666662, 0.11111111]),)
or explicitly use tuple():
a0 = tuple(np.array([0.33333332, 0.66666662, 0.11111111]))
After that you can concatenate using +=:
for i in range(0, len(a)):
a[i] += a0
Try it here.
You have a tuple (a sort of list) of numbers, and you want to make that a string. You can't replace the brackets, they aren't a part of the variable, they are just a part of its representation.
Instead you should use join() and str()
result = " ".join(str(x) for x in randy)
However, this probably will not give the right result either, as it is a nested list of tuples. You probably mean: randy = randy + (rand,) instead if randy = randy,rand.
randy = randy,rand
This notation creates a tuple of two elements, tuples do not have a method called replace.
If you just want to concatenate the numbers into a list representation separated by commas, you could do this in your loop:
randy = []
num = 0
while num < finalcount:
rand = random.randrange(1,9)
randy.append(rand)
Then you remove the replace lines, and instead do this
randy = ",".join(randy)
to get a string that has the values separated by commas.
You should maybe add the output that you would like to get to your question, because at the moment it's not quite clear what you want to do exactly.