Python基础文本冒险游戏条件分支逻辑异常问题排查
问题排查与修复
你的代码核心问题出在逻辑判断的写法错误,所有使用or的条件语句都不符合Python的语法逻辑,导致无论输入什么内容,第一个分支永远会被触发。
错误原因分析
在Python中,if direction == "left" or "l" 这种写法的逻辑是:判断direction == "left"是否为真,或者判断"l"是否为真。而非空字符串在Python中会被视为布尔值True,所以这个条件永远成立,不管direction是什么值,都会进入第一个if分支。
所有类似的判断(比如swim == "wait" or "w"、door == "yellow" or "y"等)都存在同样的问题。
正确写法
你需要明确每个or两边都是完整的比较表达式,或者更简洁的方式是用in来判断输入是否在允许的选项列表中:
方式1:完整比较表达式
if direction == "left" or direction == "l":
方式2:使用in(更简洁)
if direction in ["left", "l"]:
修正后的完整代码
print(''' ******************************************************************************* | | | | _________|________________.=""_;=.______________|_____________________|_______ | | ,-"_,="" `"=.| | |___________________|__"=._o`"-._ `"=.______________|___________________ | `"=._o`"=._ _`"=._ | _________|_____________________:=._o "=._."_.-="'"=.__________________|_______ | | __.--" , ; `"=._o." ,-"""-._ ". | |___________________|_._" ,. .` ` `` , `"-._"-._ ". '__|___________________ | |o`"=._` , "` `; .". , "-._"-._; ; | _________|___________| ;`-.o`"=._; ." ` '`."\` . "-._ /_______________|_______ | | |o; `"-.o`"=._`` '` " ,__.--o; | |___________________|_| ; (#) `-.o `"=.`_.--"_o.-; ;___|___________________ ____/______/______/___|o;._ " `".o|o_.--" ;o;____/______/______/____ /______/______/______/_"=._o--._ ; | ; ; ;/______/______/______/_ ____/______/______/______/__"=._o--._ ;o|o; _._;o;____/______/______/____ /______/______/______/______/____"=._o._; | ;_.--"o.--"_/______/______/______/_ ____/______/______/______/______/_____"=.o|o_.--""___/______/______/______/____ /______/______/______/______/______/______/______/______/______/______/_____ / ******************************************************************************* ''') print("Welcome to Treasure Island.") print("Your mission is to find the treasure.") direction = input("You arrive at a particularly ominous looking crossroad. To your left is a path that leads to a dark forest, the kind that gives you a foreboding feeling just by clasping your eyes on it. Whereas to the right there is a path to, a not much better, road with a mysteriously lit castle in the far distance. Which way do you go? Left or Right? ") direction = direction.lower() # 修正第一个判断 if direction in ["left", "l"]: swim = input("You enter the dark forest hesistantly. You hear the sound of what seems to be flowing water, and for some unknown reason you get an overwhelming urge to turn back. Reluctantly, you proceed onwards and arrive at the bank of a river where you see an island some way further down the stream. Do you try to swim across or wait to see if you can find a different way across? Swim or Wait? ") swim = swim.lower() # 修正第二个判断 if swim in ["wait", "w"]: door = input("You ponder your options for a while, until you suddenly hear the sound of song, a song that you feel you have heard before, the sort that calls you towards it even when you know logically you have never encountered it before. You follow the call of the song and arrive at a small, slightly dishevelled building with three unusual-looking doors. One is encased in a sort of viscous yellow bile with a crest portraying a proud phoenix. Another is adorned with a sort of blue glowing crystal, with a small engraved serpent at the bottom of the door. The last door is easily the most unusual of the three, with particuarly fine embellishments adorning its entire surface area, the stand-out feature is the intricately designed golden handle which constrasts the rest of the purple velvet exterior magnificently. Which door do you choose to enter? Yellow, Blue or Purple? ") door = door.lower() # 修正第三个判断组 if door in ["yellow", "y"]: print("You recognise the sign of the phoenix, the bird of immortality and rebirth and realise no danger lies behind that door. You open it and are greeted by the most wondrous sight known to man; endless treasure illuminating the room with a bright, yellow light for as far as the eyes can see. You have found the treasure! Congratulations! Now go and find some real treasure ;)") elif door in ["blue", "b"]: print("You open the door and are met with a bright blue beam of light, which pierces through your heart and you are suddenly transported to a room full of water. You are dragged down to the bottom by a ceasless pulling force where you succumb to your watery grave. Game over.") elif door in ["purple", "p"]: print("You open the door to find a proud knight standing before you. He is wearing a crown of gold and fine suit of purple armour. Initially, he stands motionless, looking at the ground. He slowly turns his gaze to you... you feel an unbearable wave of coldness wash over your entire body as his eyes meet yours. 'You do not belong here, you come seeking the treasure but you do not even know what it is you truly seek,' he lets out an exasperated sigh. 'Alas, I will end your measly life, in the hope you will reborn as someone with purpose'. He proceeds to pierce his 2-metre long sword through your neck. As you slowly bleed out, you begin to think... I should have chose a different door. Game over. ") else: print("You need to learn how to follow simple instructions. For your ineptitude you have been struck by a lightning bolt from Zeus out of sheer pity. Please try to be better. Game over.") elif swim in ["swim", "s"]: print("You enter the ice-cold water and begin to swim across. You feel the water gushing past your legs, and the tremendous resistance of the currents make you feel like you are sinking. You try to swim faster, but the currents are too strong for your meagre body to handle. You are dragged down by the currents where a giant trout swallows you whole, mistaking you for a little fish. Game over.") else: print("I thought you knew how to read. My mistake. I will end this game for you now and wait until you have the intellect to handle the simplest of instructions. If you fail to understand this statement, please contact your carer/guardian to let them explain to you what has happened. Game over.") elif direction in ["right", "r"]: print("You enter the castle, where you are greeted by a large, ominously-lit great hall. A man in a cloaked hood stands before you, his eyes glowing with a strange, unnatural light. His hood is adorned with a mysterious animal, a cross between a serpent and man. He looks upon you with utter disdain, and you feel a chill down your spine which courses through the rest of your body. He proceeds to walk towards you, and in one sudden pounce sinks his cold, black claws into your neck. You are dead. Game over.") else: print("If you cannot get past the first question without making a mistake, then you are not worthy of the treasure. May I suggest a game more suited to your abilities? Perhaps, Snakes and Ladders is more up your alley. Game over.")
额外提示
使用in判断列表成员的方式不仅代码更简洁,也更容易维护——如果后续需要添加更多输入选项,直接在列表里追加即可。
内容的提问来源于stack exchange,提问作者carlsenesque12
相关产品推荐
相关产品推荐

