Question: using python do the following I need to get a variable out from the function inside the class called place and call it to another
using python do the following I need to get a variable out from the function inside the class called place and call it to another class ill comment where it needs to be done make sure that it passes the doc tests
from __future__ import print_function class Player(object): place=None def __init__(self, name, place): """Create a player object.""" place=Player.place if place==None: place='tuc' self.name = name self.place = place self.backpack = [] def go_to(self, direction): """Go to direction if it's among the exits of player's current place. >>> swift= Place('Swift Hall', 'You are at Swift Hall', [], []) >>> bcc = Place('Bearcat Cafe', 'You are at Bearcat Cafe', [], []) >>> swift.add_exits([bcc]) >>> bcc.add_exits([swift]) >>> me = Player('player', swift) >>> me.go_to('Bearcat Cafe') You are at Bearcat Cafe >>> me.place.name 'Bearcat Cafe' >>> me.go_to('Bearcat Cafe') Can't go to Bearcat Cafe from Bearcat Cafe. Try looking around to see where to go. >>> me.go_to('Swift Hall') You are at Swift Hall """ #classa=Place() #might need to change later #if classa.self.name and direction=='Bearcat Cafe': # print("Can't go to Bearcat Cafe from Bearcat Cafe.") # print('Try looking around to see where to go.') # else: punctuations = '''!()-[]{};:'"\,<>./?@#$%^&*_~''' no_punct = "" y='You are at ' y=y.strip('') direction=direction.strip('') x=y.strip('')+direction.strip('') print(x) # I need to call that here to have a if statement saying the following if that variable == the direction it prints "Can't go to Bearcat Cafe from Bearcat Cafe. Try looking around to see where to go for char in x: if char not in punctuations: no_punct = no_punct + char Place.__init__.name=direction #if direction=='Bearcat Cafe': #x=y+direction #print(x) #return x.strip('') #elif direction=='Swift Hall': #return x.strip('') #print(x) #if Place.__init__ != direction:# might need fixed # Place.__init__.name=direction # x= Place.__init__.name # return x #else: # x="Can't go to Bearcat Cafe from Bearcat Cafe." # return x class Place(object): def __init__(self, name, description, characters, things): self.name = name #here is the variable I need self.description = description self.characters = {character.name: character for character in characters} self.things = {thing.name: thing for thing in things} self.exits = {} # {'name': (exit, 'description')} def add_exits(self, places): for place in places: self.exits[place.name] = (place, place.description)
def look(self): self.place.look() swift= Place('Swift Hall', 'You are at Swift Hall', [], [])# creates place called swift bcc = Place('Bearcat Cafe', 'You are at Bearcat Cafe', [], [])# creates place called bcc swift.add_exits([bcc])# grabs data that is contained in swift me = Player('player', swift) me.go_to('Bearcat Cafe') # goes to the playerclass and then goes to the catagory go_to #print(Place.__init__.name) this print from the class in the duction for the name of place,so class,funct,what you want print(Player.go_to)
def _test(): import doctest doctest.testmod(verbose=True)
if __name__ == "__main__": _test()
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
