Quantcast
Channel: Active questions tagged python - Stack Overflow
Viewing all articles
Browse latest Browse all 16595

How to check if the input includes an object called 'name' in a class collected in a list? Python

$
0
0

I'm making a program where you can make a city and I want it so that if you type info and then the building name which is an argument in a class. The buildings and roads created are in 2 separate lists.

Is there any way to use an 'if' or 'elif' statement to check if the name typed after 'info' is the name argument defined with user input in any of the building or road classes in the list.

So in a brief summary, if you're confused with my question, I have 2 lists. Buildings and roads. You can add either the road class or the building class depending on the list with a given input. Now I want to check if the user input is info building name (Imagine that 'building name' is the name argument in the class you added to that list) so that the info function in each class will do its thing and give details about the specific building or road as given in the code.

Here's the program:

from math import pifrom random import randint as rdclass building():    radius = None    height = None    width = None    perpendicular_height = None    base_area = None    end_area = None    popularity = rd(1,20)    was_reset = False    def __init__(self,name,shape,window_amount):        self.name = name        self.shape = shape        self.window_amount = window_amount        self.backup_volume = 0        if not str(self.name):            print('\nError! Name must be a word or phrase. Not a number!\n')        elif not str(self.shape):            print('\nError! Shape must not be a number.\n')        elif not int(self.window_amount):            print('\nError! Amount of windows must be a number.\n')        if (self.shape).lower() == 'sphere':            building.radius = int(input('\nRadius: \n'))            self.volume = (4/3)*(pi*building.radius)^3            self.backup_volume = self.volume        elif (self.shape).lower() == 'pyramid':            building.base_area = int(input('\nArea of Base: '))            building.perpendicular_height = int(input('\nPerpendicular Height: '))            self.volume = (1/3)*(building.base_area)*(building.perpendicular_height)            self.backup_volume = self.volume        elif (self.shape).lower() == 'cone':            building.base_area = int(input('\nArea of Base: '))            building.perpendicular_height = int(input('\nPerpendicular Height: '))            self.volume = (1/3)*(building.base_area)*(building.perpendicular_height)            self.backup_volume = self.volume        elif (self.shape).lower() == 'cylinder':            building.perpendicular_height = int(input('\nPerpendicular Height: '))            building.radius = int(input('\nRadius: \n'))            self.volume = ((pi*building.radius)^2)*building.perpendicular_height            self.backup_volume = self.volume        elif (self.shape).lower() == 'prism':            building.end_area = int(input('\nArea of End: '))            building.perpendicular_height = int(input('\nPerpendicular Height: \n'))            self.volume = building.end_area * building.perpendicular_height            self.backup_volume = self.volume        else:            print(f'Error! {self.shape} is not a valid shape.')    def info(self):        if was_reset == True:            return f'Volume: {self.volume}\nName: {self.name}\nWindow Amount: {self.window_amount}'        else:            return f'Volume: {building.backup_volume}\nName: {self.name}\nWindow Amount: {self.window_amount}'    def enhance(self):        enhancement = rd(1,20)        building.popularity += enhancement    def reset(self):        building.was_reset = True        building.radius = None        building.height = None        building.width = None        building.perpendicular_height = None        building.base_area = None        building.end_area = Noneclass road():    def __init__(self,name,type_,length,speed_limit):        self.name = name        self.type_ = type_        self.length = length        self.speed_limit = speed_limit        if not str(self.name):            print('\nError! Name must be a word or phrase. Not a number!\n')        elif not str(self.type_):            print('\nError! Type of road must be a word or phrase. Not a number!\n')        elif not int(self.length):            print('\nError! Length must be a number.\n')        elif not int(self.speed_limit):            print('\nError! Speed limit must be a number.\n')        if (self.type).lower() != 'highway' or (self.type).lower() != 'expressway' or (self.type).lower() != 'express way' or (self.type).lower() != 'regular':            if (self.type).lower() != 'fork':                print(f'Error! {self.type_} is not a type of road. Try something like highway or fork.')            else:                name1 = str(input('\nName of Seperate Road 1: '))                type1 = str(input('\nType of Seperate Road 1: '))                length1 = str(input('\nLength of Seperate Road 1: '))                speed_limit1 = int(input('\nSpeed Limit of Seperate road 1: \n'))                name2 = str(input('\nName of Seperate Road 2: '))                type2 = str(input('\nType of Seperate Road 2: '))                length2 = str(input('\nLength of Seperate Road 2: '))                speed_limit2 = int(input('\nSpeed Limit of Seperate road 2: \n'))                self.split_road = [road(name1,type1,length1,speed_limit1),road(name2,type2,length2,speed_limit2)]    def info(self):        return f'Name: {self.name}\nRoad Type: {self.type_}\nLength: {self.length}\nSpeed Limit: {self.speed_limit}'buildings = []roads = []print('\nHello! Welcome to CityScape, where gamer-made civilisations thrive! To start, type building to make a building\nand road to make a road! Type info then the name of a building/road to get the\ninfo about said building/road!\n')while True:    user_input = input('>> ')    if user_input == 'building':        building_name = input('Building Name: ')        building_shape = input('Building Shape: ')        building_windows = input('Amount of Windows: ')        buildings.append(building(building_name,building_shape,building_windows))        roads[-1].reset()    elif user_input == 'road':        road_name = input('Road Name: ')        road_type = input('Road Type: ')        road_length = input('Road Length: ')        road_speedlimit = input('Speed Limit: ')        roads.append(road(road_name,road_type,road_length,road_speedlimit))        roads[-1].reset()

Viewing all articles
Browse latest Browse all 16595

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>