I have a class, and a Python script that calls functions from the class.
The class is called User_Input_Test
. The script is called input_test.py
.
input_test.py
will request input from the user by using one of the class functions/methods: get_user_input(self)
. It is then supposed to print out whatever the user entered, through the use of the second function/method called show_output(self)
.
It generates an error:
User_Input_Test.show_output()\
File "/Users/michel/Python_Projects/User_Input_Test.py", line 49, in show_output\
""")
AttributeError: type object 'User_Input_Test' has no attribute 'brand'
It looks like show_output(self)
is not able to see the data pulled in from the user via get_user_input(self)
.
Would you say that is the correct interpretation of the error? And most importantly: Is there a solution for this, or am I trying to use a class for something it was never designed for?
user_input.py
:
from User_Input_Test import User_Input_Test
import time
#User_Input_Test.__init__(self, name, brand, engine, doors, fuel_type, aircon, weight, mpg, tax)
print("This little application collects data about your car")
print("Please fill out the following questionnaire:")
uname = input("What is your first name?:")
User_Input_Test.get_user_input()
print(f"{uname}, these are your car's attributes: ")
time.sleep(2)
User_Input_Test.show_output()
User_Input_Test.py
class User_Input_Test:
"""
Small Class that asks the user for their car attributes and can print them out
Attributes:
brand(string)
engine(string)
....
"""
def __init__(self, brand, engine, doors, fuel_type, aircon, weight, mpg, tax):
self.brand = brand
self.engine = engine
self.doors = doors
self.fuel_type = fuel_type
self.aircon = aircon
self.weight = weight
self.mpg = mpg
self.tax = tax
@classmethod
def get_user_input(self):
while 1:
try:
brand = input("What is the Brand & Model of your car? (e.g. 'Mercedes Benz, E-Class'): ")
engine = input("Engine Cylinders and Displacement (e.g. '4 Cylinders, 2.1 Liters'): ")
doors = input("How many doors does it have?: ")
fuel_type = input("What fuel does it use? (e.g. Petrol, Diesel, LPG): ")
aircon = input("Does it have Airconditioning? (Yes/No): ")
weight = input("How much does it weight in KG? (e.g. 1800kg): ")
mpg = input("What is the fuel consumption in Imperial MPG? (e.g. 38mpg): ")
tax = input("How much does the UK Roadtax cost per year? (e.g. £20): ")
return self(brand,engine,doors,fuel_type,aircon,weight,mpg,tax)
except:
print('Invalid input!')
continue
def show_output(self):
print(f"""
==========================================================================
Brand Name:....................... {self.brand}
Engine:........................... {self.engine}
Number of Doors:.................. {self.doors}
Fuel Type used by the engine:..... {self.fuel_type}
Does it have Aircon?:............. {self.aircon}
Fuel consumption in Imperial MPG:. {self.mpg}
Cost of Road Tax per Year:........ {self.tax}
==========================================================================
""")
User_Input_Test.show_output()
tries to call show_output
on the class itself; you need to call it on the instance returned by User_Input_Test.get_user_input()
.
from User_Input_Test import User_Input_Test
import time
print("This little application collects data about your car")
print("Please fill out the following questionnaire:")
uname = input("What is your first name?:")
car = User_Input_Test.get_user_input()
print(f"{uname}, these are your car's attributes: ")
time.sleep(2)
car.show_output()
Note: check out PEP 8, the Python style guide, notably the naming conventions for modules and classes. In this case, I would name the module car
and the class Car
for more clarity and better style. Also, the argument to a classmethod
is usually named cls
, as self
is conventionally reserved for the instance in normal methods.