简体   繁体   中英

How to create instances of a class based on user input with a while loop

I am trying to create instances of a certain class based on user input and the display their attributes this is my code:

class member_of_team:
   def __init__(self, outside_shot, inside_shot, handling, speed):
        self.outside_shot = outside_shot
        self.inside_shot = inside_shot
        self.handling = handling
        self.speed = speed          

choice = input("Would you like to enter a teamate? ")
y = choice
while  y == "yes":

    x = input("what is the teamates name? ")
    a = int(input("How good is he at shooting outside? "))
    b = int(input("What about his inside shot? "))
    c = int(input("How well is he at handling the ball? "))
    d = int(input("How fast is he? "))
    x = member_of_team(a, b, c, d)
    y = input("Do you want to enter another teamate? ")

r = input("what member of the team woulsd you like to check up")
s = input("what would you like ot know about him")

print(r.s)

AttributeError: 'str' object has no attribute 's'

I believe you were trying to create an object from member_of_team class. And later you were giving the object's name as input and then trying to access the attributes of that object.

You can use getattr() builtin function of Python.

getattr(object, name[, default])

Return the value of the named attribute of object. name must be a string. If the string is the name of one of the object's attributes, the result is the value of that attribute. For example, getattr(x, 'foobar') is equivalent to x.foobar. If the named attribute does not exist, default is returned if provided, otherwise AttributeError is raised.

So, you need maintain a directory of all the objects created. Because in python objects were created on the fly. And when you provide object's name from STDIN. You get that in that in the form of String. You need to find out out the its equivalent OOP's object actually.

Please check out this code.

class member_of_team(object):
   def __init__(self, outside_shot, inside_shot, handling, speed):
        self.outside_shot = outside_shot
        self.inside_shot = inside_shot
        self.handling = handling
        self.speed = speed

choice = input("Would you like to enter a teamate? ")
object_directory = {}

while  choice == "yes":
    x1 = input("what is the teamates name? ")
    a = int(input("How good is he at shooting outside? "))
    b = int(input("What about his inside shot? "))
    c = int(input("How well is he at handling the ball? "))
    d = int(input("How fast is he? "))
    x = member_of_team(a, b, c, d)
    object_directory[x1] = x
    y = input("Do you want to enter another teammate? ")

r = input("what member of the team would you like to check up?")
s = input("what would you like to know about him?")


print (getattr(object_directory[r], s))

Sample Console Output:

Would you like to enter a teamate? yes
what is the teamates name? Ronaldo
How good is he at shooting outside? 5
What about his inside shot? 7
How well is he at handling the ball? 8
How fast is he? 8
Do you want to enter another teammate? no
what member of the team would you like to check up?Ronaldo
what would you like ot know about him?handling
8

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM