| 208 | |
| 209 | |
| 210 | def askForPlayerMove(maxLevelNum): |
| 211 | # Get the input from the player: |
| 212 | while True: |
| 213 | move = input('Enter move> ').upper() |
| 214 | |
| 215 | if move == 'QUIT': |
| 216 | print('Thanks for playing!') |
| 217 | sys.exit() |
| 218 | |
| 219 | if move.isdecimal(): |
| 220 | if not (1 <= int(move) < maxLevelNum): |
| 221 | # There's no level for the number the player entered: |
| 222 | print('Level numbers are between 1 and', maxLevelNum) |
| 223 | continue |
| 224 | return str(int(move) - 1) # Return the new level number. |
| 225 | |
| 226 | # Check that the input is a valid WASD move or U for undo: |
| 227 | if move in ('W', 'A', 'S', 'D', 'U'): |
| 228 | return move |
| 229 | |
| 230 | print(move, 'is not valid. Enter W, A, S, D, UNDO, or QUIT.') |
| 231 | |
| 232 | |
| 233 | # If this program was run (instead of imported), run the game: |