-4

I just started learning Python a couple of days ago as my first language and I ran into some trouble while trying to make a game. Here's the part I'm stuck at: Primary concern: I want it to randomly generate objects and doors. Secondary concern: I need each room to remember the objects even after I leave the room (Pots are destructible)

The problem: I want the program to spit out the number of pots,chests,superchests and doors , but the variables remain empty. Sorry for not framing the question more specifically, but I just started and the water is kinda murky in here D:

def random_room(pot, chest, schest, ldoor, rdoor, fdoor):
    import random
    loop = 0
    pot = 0
    chest = 0
    schest = 0
    ldoor = 0
    rdoor = 0
    fdoor = 0
    while loop < 6:       
        rand = random.randint(0, 30)
        if rand  in range(1, 3, 1):
            chest += 1
            loop += 2
            return chest
        if rand in range(4,10, 1):
            schest -= 1
            return schest
        if rand == 16:
            schest += 1
            loop += 3
            return schest
        if rand > 16:
            pot += 1
            loop +=1
            return pot
        if rand in range(10,12, 1):
            ldoor = 1
            return ldoor
        if rand in range(12,14, 1):
            fdoor = 1
            return fdoor
        if rand in range(14, 16, 1):
            rdoor = 1
            return rdoor
        if schest < 0:
            schest = 0
        if rdoor + fdoor + ldoor == 0:
            rand = random.randint(1,3)
            if rand == 1:
                rdoor += 1
            if rand == 2:
                ldoor += 1
            if rand == 3:
                fdoor += 1

random_room(pot, chest, schest, ldoor, rdoor, fdoor)
print pot
print ldoor
print rdoor
print fdoor
print chest
print schest
room = 2
while room == 2:    
    left_door = ""
    right_door = ""
    front_door = ""
    print "You enter a room."
    if chest == 1:
        print "There is one CHEST in the room."
    if chest > 1:
        print "There are", chest, "CHESTs in the room."
    if pot == 1:
        print "There is one POT in the room."
    if pot > 1:
        print "THere are", pot, "POTs in the room."
    if ldoor == 1:
        left_door = "a door to the LEFT"
    if rdoor == 1:
        right_door = "a door to the RIGHT"
    if fdoor == 1:
        front_door = "a door in the FRONT"
    if True:
        print "There's", left_door, ",", right_door, ", and", front_door
        break
Sunwoo Yang
  • 1,213
  • 2
  • 12
  • 22
  • 3
    What trouble are you having? I don't think anyone can help you unless you ask a specific question. – obmarg Feb 10 '12 at 20:06
  • 4
    Daniel, you might want to learn more about Python's classes before trying to tackle writing a game of this nature. The code that you've written here looks like someone trying to translate BASIC directly to Python and missing out on all of the object-oriented goodness that comes with it. – Adam Crossland Feb 10 '12 at 20:07
  • Is there a good simple online resource for learning about object oriented programming? Thanks for the answers btw. – Sunwoo Yang Feb 10 '12 at 20:09
  • When I run the code, I want it to spit out the number of pots, chests, superchests and doors, but the variables remain empty. – Sunwoo Yang Feb 10 '12 at 20:09
  • 1
    There are dozens if not hundreds. Put 'object oriented programming with python' into Google and try different ones. You'll find one that works for you, and investing the time to try out several will be time very well-spent. – Adam Crossland Feb 10 '12 at 20:11
  • 1
    @Daniel: Welcom to SO, this is a Q&A site, you got some down-votes to your question, maybe you want to [edit](http://stackoverflow.com/posts/9234115/edit) it and improve it following some of the advice in this [Style guide for questions and answers](http://meta.stackexchange.com/a/18616/177799) :) – Rik Poggi Feb 10 '12 at 20:12
  • Maybe you could start by describing, in English, in detail, what you want the code to do. – Karl Knechtel Feb 10 '12 at 23:02

2 Answers2

3

Daniel, there are many problems with code that you have posted. I'll try to outline as many of them as I can.

First, parameters that are passed to a function generally provide a means of getting information into a function, not out of a function. Python does allow you to return multiple values from a function however, so instead of this:

random_room(pot, chest, schest, ldoor, rdoor, fdoor)

you want to say this:

pot, chest, schest, ldoor, rdoor, fdoor = random_room()

The next big problem is that return immediately exits a function, so when, inside your random_room function, you say:

 while loop < 6:       
        rand = random.randint(0, 30)
        if rand  in range(1, 3, 1):
            chest += 1
            loop += 2
            return chest

The return chest immediately exits the function and returns only the value in the variable chest which will be given to the variable pot. However, if you remove all of the return statements that are inside the while loop, the loop will finish executing, and at the end, you can say:

return pot, chest, schest, ldoor, rdoor, fdoor

and all of the values will be returned to the code that called the random_room function.

Finally, this code:

room = 2
while room == 2:

is effectively meaningless and will only result in an infinite loop. Since there is no code inside the while loop that changes the value of the variable room, it will just keep printing over-and-over-and-over again. I think that you might have wanted to have the loop run a couple of times and print out the values for a couple of calls to random_room?

If that's the case, you probably want the code to look something like this:

room = 0
while room < 5: # Print five calls to random room
    pot, chest, schest, ldoor, rdoor, fdoor = random_room()
    # Code to print out values returned from random_room
    # ...
    room += 1

As I said in the comments above, you need to do a lot more reading on Python and object-oriented Python. Python can be a very forgiving language in that it might not give you explicit errors when you try to run code that is syntactically legal but might not make much sense at all. Fortunately for you, python is very popular language, and there are a myriad of helpful resources on the web that you can take advantage of.

Best of luck.

Adam Crossland
  • 14,198
  • 3
  • 44
  • 54
  • Wow thanks for taking the time to read through the code and show me the corrections! All the information helped a lot and I'm starting to read on classes and object oriented programming. A random side question, if you don't mind: when should i start learning django? Is there a minimum amount of python I need to know to move onto the web framework? – Sunwoo Yang Feb 10 '12 at 21:49
  • @DanielSunYang, I'm not very experienced with Django, but I would definitely think that you would want to have a comfortable command with Python before venturing there. – Adam Crossland Feb 10 '12 at 22:36
  • @DanielSunYang: "Is there a minimum amount of python I need to know to move onto the web framework". Yes. All of the language. Not all of the libraries. – S.Lott Feb 10 '12 at 23:35
0

Not sure I understand what you're asking, but this thread may help:

How do I pass a variable by reference?

Community
  • 1
  • 1
Rich.Carpenter
  • 1,056
  • 1
  • 9
  • 21