I am trying to pass a text entry to print a specific pre-defined class entry, but keep getting the error
AttributeError: 'str' object has no attribute 'customerid'
I'm new to Python and this is part of my masters, so it's a steep learning curve for me :-D
class Customer:
def __init__(self, customerid, name, address, phone, email = None):
self.customerid = customerid
self.name = name
self.address = address
self.phone = phone
self.email = email
simon = Customer("SIM001","Simon","My Address","1234565789","[email protected]")
bob = Customer("BOB001","Bob","The Cobbles","808808808","[email protected]")
kermit = Customer("KER001","Kermit the Frog","Sessame Street","00000000","[email protected]")
request = str(input("enter a name of either simon, bob or kermit"))
print(request.customerid)
print(request.name)
print(request.address)
print(request.phone)
print(request.email)
CodePudding user response:
What you are doing is taking input from the user and storing it in request variable which is an str and not connected to the Customer class in any way, what you should do is probably this:
if request=="simon":
print(simon.customerid)
....
and so on for other values
CodePudding user response:
input returns a string and you want a class.
there are a lot of ways to do that and I will show you a simple one.
class Customer:
def __init__(self, customerid, name, address, phone, email = None):
self.customerid = customerid
self.name = name
self.address = address
self.phone = phone
self.email = email
simon = Customer("SIM001","Simon","My Address","1234565789","[email protected]")
bob = Customer("BOB001","Bob","The Cobbles","808808808","[email protected]")
kermit = Customer("KER001","Kermit the Frog","Sessame Street","00000000","[email protected]")
request = input("enter a name of either simon, bob or kermit")
requestedCustomer = None
if request == 'simon':
requestedCustomer = simon
elif request == 'bob':
requestedCustomer = bob
elif request == 'kermit':
requestedCustomer = kermit
if requestedCustomer:
print(requestedCustomer.customerid)
print(requestedCustomer.name)
print(requestedCustomer.address)
print(requestedCustomer.phone)
print(requestedCustomer.email)
else:
print('customer not found')
CodePudding user response:
Many thanks everyone, I found an easier way
request = eval(input("enter the name of either simon, bob or kermit "))
that then allows me to use
print(request.customerid)
So my code now looks like this:
class Customer:
def __init__(self, customerid, name, address, phone, email = None):
self.customerid = customerid
self.name = name
self.address = address
self.phone = phone
self.email = email
simon = Customer("SIM001","Simon","My Address","1234565789","[email protected]")
bob = Customer("BOB001","Bob","The Cobbles","808808808","[email protected]")
kermit = Customer("KER001","Kermit the Frog","Sessame Street","00000000","[email protected]")
request = eval(input("enter a name of either simon, bob or kermit "))
print(request.customerid)
print(request.name)
print(request.address)
print(request.phone)
print(request.email)
CodePudding user response:
Try this code
request = str(input("enter a name of either simon, bob or kermit"))
customer_list = [simon,bob,kermit]
for customer in customer_list:
print(customer.name)
if customer.name.lower() == request:
print(customer.customerid)
print(customer.name)
print(customer.address)
print(customer.phone)
print(customer.email)