1. Limited time only! Sign up for a free 30min personal tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Python syntax

  1. Dec 3, 2009 #1
    I'm doing the movement part of the wumpus game. I'm sure many of you are familiar with it, but if not; basically the player starts out in room 1 and s allowed to move into any adjacent room, then from that room into any adjacent room and so on .

    The map files that are organized as such:
    and so on

    so lines 1 and 3 are the room numbers and lines 2,4 are adjacent rooms
    This is what I have so far. btw the map files are text format.

    The attempt at a solution
    Code (Text):
    from wumpglobal import *
    f = open("map1", "r")
    i = 0
    everything = f.readlines()[COLOR="DarkRed"]<-----This turns all map elements into a list[/COLOR]
    start_room = everything[i][COLOR="DarkRed"]<---room player is in[/COLOR]
    close_rooms = everything[i+1][COLOR="DarkRed"]<-----adjacent rooms to start_room[/COLOR]

    def movement(i):
        if room != item in list(close_rooms):[COLOR="DarkRed"]<-----Whats the correct syntax for this?[/COLOR]
            return room + "is not an adjacent room"
            i = room + 2
    while room != "quit":[COLOR="DarkRed"]<----This is the loop that runs until user inputs "quit"[/COLOR]
        print "You are in room: " + start_room
        print "Adjoining rooms: " + close_rooms

        room = raw_input("Enter a room number to move, or q) quit")
        print movement(i)

    Am I on the right track here? I worked out the problem on paper, and it seems like I have all the right concepts in code. I'm just confused about the arrangement. (integrating the loop and movement function)

    Thanks in advance
  2. jcsd
  3. Dec 4, 2009 #2
    The correct method for finding an item in Python:

    Code (Text):
    if needle in haystack {
      do something
    } else {
      do something else
    In your code:

    Code (Text):
    if i in close_rooms:
    At least, I take it that i in def movement is a local variable. My Python ain't good.

    Shouldn't you also say 'print movement(room)' instead of 'print movement(i)'? You don't set i anywhere in your while loop.

    The everything variable seems to be an array. Correct me if I'm wrong, but I believe that every even element is a room and every odd element are the rooms that are adjacent to that room. In short, every line in your maps file has its own cell in the array, in the order in which they've been noted.

    Following that assumption: I don't think that you ever have the guarantee that room + 2 is the new room the player entered. Following your example: Say I'm in room 1 and I enter 275. It's a valid room, but won't I end up in room 2 according to your i = room + 2 assignment?

    You're on the right track, though.

    You'll want to add an if-statement to your while-loop, though. You can't move to the room labeled 'quit'. :p
  4. Dec 4, 2009 #3
    wow, thanks that was quite helpful.

    Ya, I get what you are saying about the room+2 thing.

    i think it should be i + room.
    room being the input(room you wanna go to) and i bring the room currently in.

    My mistake, the map file is actually like this:

    but that shouldn't make a difference.
  5. Dec 4, 2009 #4
    Hmmmm... I still get a error after the changes.

    it says room is not defined on the line
    Code (Text):
    while room!= "q"
  6. Dec 6, 2009 #5
    Right, that's because on the first iteration of the while loop, Python doesn't yet know what the heck the room variable is. To fix this, just add this line of code to your program a second time, but this time add it before the while loop:

    room = raw_input("Enter a room number to move, or q) quit")

    That way Python will know what room is before the while loop first iterates.
  7. Dec 6, 2009 #6

    Code (Text):
    ''' assumes that the initial room number is 1 '''

    from wumpglobal import *
    import sys <---- optional

    def movement(room, current_room):
        text = ''
        if room in dict[current_room]:
            text =  'You are now in room number %f /n' %room
            current_room = room
            if room in dict and dict[room] != []:
                adjacent = str(dict[room])
                text += 'The adjacent rooms are: %s' %adajcent

                ## this is kind of lame, the str representation of a list, you could easily have
                ## string repr's of the elements instead though

             elif dict[room] == []:
                 text += 'There is nowhere else to go'
             text +=  'This is room is not adjacent to your current location'

         print text
         return current_room

    if __name__ == '__main__':

        f = open("map1", "r")
        dict = {}
        line_list = []
        other_list = []
        current_room = 1

        for line in f:
            line_list = line.split(,)
            if len(line_list) > 1:
                room = line_list[0]
                dict[room] = []

        loop = 1
        while 1 OR loop == 1:
            input = raw_input("Please enter an adjacent room number or type 'quit' to quit:")
            if input == 'quit':
                sys.exit(0) OR loop = 0
                current_room = movement(input, current_room)

    this is a rough idea of what I think should be done, you should have something that sorts out good and bad input. Also, you probably want to sort your rooms and adjacent rooms into a dictionary, since there are keys and corresponding values (rooms/adjacent roooms).
    There are probably mistakes but this is rough

    hope this helps
    Last edited: Dec 7, 2009
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Similar Discussions: Python syntax
  1. VHDL syntax error (Replies: 2)

  2. Matlab syntax (Replies: 2)