I am creating a game with three different rooms with three different functions. The game needs to be started in the living room but the living room function will not run it only will start in the attic() function. Another problem I am having is having the other functions being called to change the room that the player is in. I use the selection to change the room but it will not change to the right room. I am sure there are other bugs in my program but I need to solve these two problems first. `
def living_room():
Gamewon = False
Mouse = ""
Inventory = ""
Hole = ""
Location = "living room"
while Location == "Living room":
print("")
print("Room: living room")
print("You are in the living room of the house. You see in this Room:")
print("A pot of soil")
print("Stairs going to the second floor")
print("A dark doorway")
print("A ball of string")
print("")
print("Room actions:")
print("(l)ook: go closer to look at the pot of soil")
Inventory = ""
if Inventory == "" or Inventory == "Cheese":
print("(p)ick up: ball of string")
print("(g)o: through the dark doorway")
print("(u)p: take the stairs to the second floor")
selection = input("Your selection: ")
if selection in "l":
print("The soil looks very dry")
elif selection in "l" and Mouse == "full":
print("The growing beanstalk takes you away to saftey!")
Gamewon = True
elif Inventory == "" and selection in "p":
Inventory = "String"
print("You have picked up the string")
elif selection in "g":
Location = "bed room"
elif selection in "u":
print("You take the stairs to the attic")
Location = "attic"
else:
print("Please make a proper selection")
def attic():
Gamewon = False
Inventory = ""
Hole = ""
Location = "attic"
while Location == "attic":
print("")
print("Room: Attic")
print("You have entered the attic of the house. In this room there is:")
print("A large assortment of random cheeses being stored up here.")
print("A hole much smaller then the pieces of cheese.")
print("")
print("Room actions:")
print("(c)heese: Pick up cheese and attempt to drop it in the hole.")
if Inventory == "String" or Inventory == "String,Cheese":
print("(s)tring: Drop the string into the hole.")
print("(p)ick up: Pick up a piece of cheese and keep it.")
print("(d)own: Go down the stairs.")
selection = input("Your selection: ")
if selection in "c":
print("The cheese does not fit down this hole.")
elif selection in "s":
print("The string has disappeared down the hole.")
Hole = "DroppedString"
elif selection in "p":
print("You have picked up a piece of cheese.")
Inventory = "Cheese"
elif selection in "d":
print("You have gone down the stairs.")
Location = "living room"
else:
print("Please make a proper selection")
def bed_room():
Gamewon = False
Hole = ""
Inventory = ""
Location = "bed room"
while Location == "bed room":
print("")
print("Room: Bedroom")
print("You have entered the bedroom.")
if Hole == "":
print("In the room there is the usual furnishings along with a cat staring at a mouse hole.")
if Hole == "DroppedString":
print("In the room there is the usual furnishings along with a mouse in front of the mouse hole.")
print("")
print("Room actions:")
print("(g)o through the entrance way.")
if Inventory == "String":
print("(u)se ball of string to play with cat.")
elif Hole == "DroppedString" and Inventory == "Cheese":
print("(f)eed the mouse the cheese")
selection = input("Your selection: ")
if selection in "g":
print("You have left the bedroom.")
Location = "living room"
elif selection in "u":
print("The cat looks and the string but then goes back to the mouse hole.")
elif selection in "f":
print("The mouse happily eats the cheese and scurries out of the room.")
Mouse = "full"
else:
print("please make a proper selection")
def start():
Location = "attic"
Gamewon = False
while Gamewon == False:
if Location == "attic":
attic()
elif Location == "living room":
living_room()
elif Location == "bed room":
bed_room()
while Gamewon == True:
print("Congratulations you have won the game!")
start()
I have tried rewriting the function and going through it to figure out what is different from the other functions but I am relatively new to coding so I cannot see what is wrong everything I have tried so far has just created more problems.
CodePudding user response:
the reason your game isn't starting in the living room is because in def start()
you have set the Location = "attic"
.
Change that to Location = "living_room"
and it will call def living_room()
. As Code_Apprentice says, the Location
variable in def start()
will not be "seen" in any other functions, it is only local to that function.
Also,
Location = "living room"
while Location == "Living room":
Your code will not go into the while loop as you have used an upper case "L". It is case sensitive so probably change the while loop condition to lower case "l".
You've got some other issues with the code but part of the achievement is working things out. I hope this gets your code to start where you want it.
CodePudding user response:
There are several things wrong with your code. At the root of all of them is that you set Location == 'attic'
in start()
and it never changes. Setting Location = ...'
something in your other functions just creates a new local variable for that function. It does not change Location
in the start()
function. You should read more about variables and scope. You should also read about passing parameters and returning values from your functions. These are all tools you will need to make your code work the way you want.