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 'format'
ERROR: AttributeError: 'tuple' object has no attribute 'to'
'tuple' object has no attribute 'to' in pytorch
Python AttributeError: 'tuple' object has no attribute 'print' - Stack Overflow
You need to create instances of your class:
Enemies = []
i = 0
while (i < 9):
randomhealth = random.randrange(125,300,25)
randomdamage = random.randrange(25,100,25)
randomammo = random.randrange(20,200,20)
# create an Enemy - not a tuple of values
new_enemy = Enemy( "Enemy {}".format(i), randomhealth, randomdamage, randomammo)
Enemies.append(new_enemy)
i += 1
for Enemy in Enemies:
Enemy.properties()
If you create a __str__(self) method in your class you can "tell" python how to print an instance of your class:
class Enemy:
# snipped what you already had
# is used if you print(instance)
def __str__(self):
return """Properties:
Name: {}
Health: {}
Damage: {}
Ammo: {}""".format(self.name, self.health, self.damage, self.ammo)
# is used by lists if you print a whole list
def __repr__(self):
return str(self)
Read about __str__ here: python __str__ for an object
Okay, first off, don't do shenanigans like
i = 0
while (i < 9):
i++
// bad
use
for i in range(9):
// good
instead.
Second off, with
new_enemy = ("Enemy" + str(i+1),randomhealth,randomdamage,randomammo)
Enemies.append(new_enemy)
you're appending a Tuple to your Entities list. How could you possibly expect that Tuple to have the properties of your Entity class?
So what you want to use instead is
Enemies = []
for i in range(9):
randomhealth = random.randrange(125,300,25)
randomdamage = random.randrange(25,100,25)
randomammo = random.randrange(20,200,20)
new_enemy = Enemy("Enemy" + str(i+1), randomhealth, randomdamage, randomammo)
Enemies.append(new_enemy)
for Enemy in Enemies:
Enemy.print()
Next, in your last for loop, you're using the class name Enemy as a variable in the loop. Terrible idea, but luckily you can just make that lowercase. So, why does it STILL say that your Enemy class has no print member? Because you haven't defined one, you called the method "properties" instead.
for enemy in Enemies:
enemy.properties()
And it works. However, since we're in python, we can simplify this code quite a lot.
Endresult:
import random
class Enemy:
def __init__(self,name,health,damage,ammo):
self.name = name
self.health = health
self.damage = damage
self.ammo = ammo
def __str__(self):
return "Properties:\nName: {}\nHealth: {}\nDamage: {}\nAmmo: {}".format(
self.name, self.health, self.damage, self.ammo)
#your other methods
Enemies = [Enemy(name="Enemy" + str(i),
health=random.randrange(125, 300, 25),
damage=random.randrange(25, 100, 25),
ammo=random.randrange(20, 200, 20))
for i in range(1, 10)]
for enemy in Enemies:
print(enemy)
I had a different problem that caused this error. My code was attempting to get the value of a cell in row zero (I ran the loop one row too far).
Row zero doesn't exist (sheets begin at row 1), hence the AttributeError: 'tuple' object has no attribute 'value' error.
Problem seems to be with sheet['Ax'] that you are doing. You won't be able to use x this way.
Try modifying your indexes like following in the code.
title=sheet['A' + str(x)].value
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?