I am getting errors when attempting to use the random module. Specifically, I am trying to use choice() with a list of rooms (a class I've defined) but python isn't wanting to do it. I have had issues importing random in the past as well and it never wants to let me just put in 'import random', but last time it let me do the whole 'from random import...'. I don't have any files named random and I've tried several different things to try to fix it but none have worked and I am very confused. plz help.
The error I am getting is
Traceback (most recent call last):
File "/Users/mbwright/Desktop/moster_capture.py", line 82, in <module>
class Wanderer(Movable):
File "/Users/mbwright/Desktop/moster_capture.py", line 87, in Wanderer
rand_room = List_of_rooms.choice()
AttributeError: 'list' object has no attribute 'choice'
Here is the code im working with, the error im getting is from specifically:
rand_room = List_of_rooms.choice()
This is near the end in the Wanderer class.
from random import choice
class Room:
"""Information about the location.
Attributes: room_name (str), room_number (int)
***Non-Public attributes: rooms_connected (set of rooms)"""
def __init__(self, room_name, room_number):
self.room_name = room_name
self.room_number = room_number
self.__rooms_connected = []
def __repr__(self):
return 'Room("' + self.room_name + '", ' + str(self.room_number) + ")"
def connect_to(self, others):
if isinstance(others, list):
for room in others:
if room not in self.__rooms_connected:
self.__rooms_connected.append(room)
room.__rooms_connected.append(self)
else:
if self not in others.__rooms_connected:
self.__rooms_connected.append(others)
others.__rooms_connected.append(self)
def get_connections(self):
return self.__rooms_connected
def is_connected_to(self, other):
if other in self.__rooms_connected:
return True
else:
return False
def description(self):
return "You are in room " + str(self.room_number) + ", " + self.room_name
##########Rooms############
Crawl = Room("Crawl Space", 1)
Scarlet = Room("The Scarlet Room", 2)
Cellar = Room("The Cellar", 3)
Guards = Room("The Guard Room",4)
Armory = Room("The Armory", 5)
Passage = Room("The Dimly Lit Passage",6)
Passage.connect_to([Armory, Crawl, Scarlet])
Cellar.connect_to([Armory, Crawl, Scarlet])
Guards.connect_to([Armory, Crawl, Scarlet])
List_of_rooms = [Crawl, Scarlet, Cellar, Guards, Armory, Passage]
###########################
class Command:
"""Stores the actions and rooms
Attributes: action (str: "move" or "shoot"), destination (Room)"""
def __init__(self, action, destination):
self.action = action
self.destination = destination
def __repr__(self):
return "Command(" + self.action + ", " + str(destination) + ")"
###########################
class Movable:
"""Represents objects and characters that have changable locations.
Non-Public: __location"""
def move_to(self, new_location):
self.__location = new_location
def __init__(self, location):
self.move_to(location)
def __repr__(self):
return "Movable(" + str(self.__location) + ")"
def update(self):
pass
###########################
class Wanderer(Movable):
"""Things that will move randomly on their own
Attributes: self.awake (bool)"""
rand_room = List_of_rooms.choice()
def __init__(self, location = rand_room, awake = True):
self.awake = awake
self.move_to(location)
def update(self):
if self.awake == True:
potentials = self.get_connections()
pick = potentials.choice()
self.move_to(pick)
self.awake = False
Aucun commentaire:
Enregistrer un commentaire