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
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
returnimmediately exits a function, so when, inside yourrandom_roomfunction, you say:The
return chestimmediately exits the function and returns only the value in the variablechestwhich will be given to the variablepot. However, if you remove all of the return statements that are inside thewhileloop, the loop will finish executing, and at the end, you can say:return pot, chest, schest, ldoor, rdoor, fdoorand all of the values will be returned to the code that called the
random_roomfunction.Finally, this code:
is effectively meaningless and will only result in an infinite loop. Since there is no code inside the
whileloop that changes the value of the variableroom, 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 torandom_room?If that’s the case, you probably want the code to look something like this:
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.