I started to code a text-based game in a
Main program which call functions in file-modules.
There is a particular case where a file-module and a function are called and the function (in the module) should append a list in the Main() program (a global variable).
The problem is it cannot append it.
So, more concretely, in the game, the player enters a room (RoomA2) and there’s an object (sword).
The player can pick it up (and I want the function to remove it from the list called object_room_a2 ) and put the sword in his bag.
So, the bag is a global list (called object_list) in the Main() program.
Please the codes below, thanks for the help!
object_list = []
def main():
print('stuff.. What do you want to do? (1) do this; (2) do that')
choice = int(input('Choice --> '))
if choice == 1 :
RoomA2.room_a2()
elif choice == 2 :
RoomB1.room_b1()
def room_a2():
object_room_a2 = ['sword']
print('stuff.. What do you want to do? (1) do this; (2) do that')
choix = int(input('choix --> '))
if choix == 1:
object_room_a2.remove('sword')
object_list.append('sword')
if choix == 2:
RoomA1.room_a1()
The problem is that every time
room_a2is called,object_room_a2is re-initialized so the sword will keep reappearing.In general, this is a very complicated way to do what you are trying to do. A player object and a collection of room objects would make for far less code, but I assume that you are a beginner and might want to get a few rooms working this way first.