[英]Why is my exit statement not working properly?
目標是在這個簡化版本的文本基礎游戲中的房間之間移動。 該代碼完全按計划工作,除非您嘗試在輸入“說明”后直接輸入“退出”。 輸入“指令”后,第一個“退出”在 else 無效語句中運行,然后第二個“退出”輸入按預期退出游戲。 如果您在“說明”之后繼續至少一個輸入,那么退出也能正常工作。
rooms = {
'Great Hall': {'South': 'Bedroom'},
'Bedroom': {'North': 'Great Hall', 'East': 'Cellar'},
'Cellar': {'West': 'Bedroom'}
}
def instruction():
"""Function to give instructions on how to play the game"""
print('Welcome to Module 6 Milestone')
print('Move commands are go North, go South, go East, go West')
print('Typing exit will exit the game')
print('Inputting instructions will remind you of the game instructions')
print('Good luck may the odds be in your favor')
def invalid():
"""Function for if an invalid input is entered"""
print('------------')
print('Whoops invalid command, try again')
print('------------')
def main():
"""Main function that runs the movement between rooms"""
current_room = 'Great Hall'
print('\nYou are starting in the', current_room)
move = input('What will you do next?\n>').split()
directions = ['North', 'South', 'East', 'West'] # directions in the dictionary
while True:
if len(move) < 2: # for one word inputs
if 'exit' in move: # exit the game
print('\nThanks for playing!')
break
elif 'instructions' in move: # reprint instructions
instruction()
print('------------')
print('\nYou are in the', current_room)
else:
invalid()
print('You are still in the', current_room)
move = input('\nWhat will you do next?\n>').split() # next move input
if len(move) == 2: # 2 word inputs
if move[1] in directions: # checks if move is a valid direction
if move[1] in rooms[current_room]: # if move is a valid direction in current room
current_room = rooms[current_room][move[1]] # changes current room if valid
print('------------')
print('You have found the', current_room)
elif move[1] not in rooms[current_room]: # if move in directions but not a valid move in current room
print('------------')
print('Oh no it seems to be a dead in')
print('You are still in the', current_room)
else: # not a valid move command
invalid()
print('You are still in the', current_room)
move = input('What will you do next?\n>').split() # next move input
else: # invalid move command
invalid()
print('You are still in the', current_room)
move = input('What will you do next?\n>').split()
instruction() # prints instructions function when game runs
print('------------')
if __name__ == '__main__': # if code is not imported than main() will run
main()
在你的線之后
move = input('\nWhat will you do next?\n>').split() # next move input
您應該使用continue
跳回到循環的開頭。
while True:
if len(move) < 2: # for one word inputs
# handle the input in various ways
move = input('\nWhat will you do next?\n>').split() # next move input
if len(move) == 2: # 2 word inputs
輸入單字命令時,在第一個if
塊中處理,然后輸入新的 move。
問題是因為第二個塊是if
而不是elif
,它會立即處理新的移動。
一般來說,最好只在靠近循環頂部的一個地方請求用戶輸入:
while True:
move = input("...").split()
if len(move) < 2:
# handle it
elif len(move) == 2:
# handle it
else:
# handle it
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.