Python while statment doesnt work -
this question exact duplicate of:
i have homework question. should create dictionary represent following:
north leads garden. south leads kitchen. east leads dining room. west leads living room.
the player should prompted direction , respond location off in direction. example, if player enters north, program should respond: north leads garden. if player enters invalid direction, program should ignore input , ask direction. program end when player enters quit.
my problem when user enter "quit" program doesn't exit. dnt understand why while statement not working. here code:
#create dictionary represent possible #exits location in adventure game game = {"north" : "north leads garden.", "south" : "south leads kitchen.", "east" : "east leads dining room.", "west" : "west leads living room."} print "press quit exit" direction = raw_input("enter direction: ") while direction != "quit": direction = direction.lower() if direction in game: location = game[direction] direction = direction.lower() print location if direction not in game: direction = raw_input("enter direction: ") location = game[direction] direction = direction.lower() print location raw_input("\n\npress quit exit")
as others here, can't entirely sure of code trying because of lack of indents, taking shot in dark, may easier use method getting direction handle bad directions. code can become:
#create dictionary represent possible #exits location in adventure game def get_dir(): good_answers = ["north", "south", "east", "west", "quit"] direction = raw_input("enter direction: ").lower() while direction not in good_answers: direction = raw_input("bad direction, try again: ").lower() return direction game = {"north" : "north leads garden.", "south" : "south leads kitchen.", "east" : "east leads dining room.", "west" : "west leads living room."} print "press quit exit" direction = get_dir() while direction != "quit": print game[direction] direction = get_dir() print "quitting..."
Comments
Post a Comment