我正在創建一個包含三個不同房間的游戲,這些房間具有三種不同的功能。游戲需要在客廳啟動,但客廳功能不會運行,它只會在 attic() 函式中啟動。我遇到的另一個問題是呼叫其他函式來更改玩家所在的房間。我使用選擇來更改房間,但它不會更改為正確的房間。我確信我的程式中還有其他錯誤,但我需要先解決這兩個問題。`
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()
我已經嘗試重寫該函式并通過它來找出與其他函式的不同之處,但我對編碼相對較新,所以我看不出有什么問題,到目前為止我所嘗試的一切都產生了更多問題。
uj5u.com熱心網友回復:
你的游戲沒有在客廳開始的原因是因為def start()你已經設定了Location = "attic".
將其更改為Location = "living_room",它將呼叫def living_room(). 正如 Code_Apprentice 所說,Location變數 indef start()不會在任何其他函式中“看到”,它只是該函式的本地變數。
還,
Location = "living room"
while Location == "Living room":
您的代碼不會進入 while 回圈,因為您使用了大寫“L”。它區分大小寫,因此可能將 while 回圈條件更改為小寫“l”。
您的代碼還有其他一些問題,但部分成就是解決問題。我希望這能讓你的代碼從你想要的地方開始。
uj5u.com熱心網友回復:
您的代碼有幾處問題。Location == 'attic'所有這些的根源是你設定start()并且它永遠不會改變。在其他函式中設定Location = ...'某些內容只會為該函式創建一個新的區域變數。Location它在功能上沒有變化start()。您應該閱讀有關變數和范圍的更多資訊。您還應該閱讀有關從函式傳遞引數和回傳值的資訊。這些都是使您的代碼按照您想要的方式作業所需的工具。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/532046.html
標籤:Python功能
上一篇:通用功能問題
