From 37d82b6d56f5db0e493418aef1bf925187658112 Mon Sep 17 00:00:00 2001 From: Daniel Piron Date: Sat, 20 Feb 2021 13:00:34 -0500 Subject: [PATCH] Extract function for prompting user input --- 11 Bombardment/python/bombardment.py | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/11 Bombardment/python/bombardment.py b/11 Bombardment/python/bombardment.py index 0552f0d2..4320803a 100755 --- a/11 Bombardment/python/bombardment.py +++ b/11 Bombardment/python/bombardment.py @@ -36,24 +36,37 @@ def generate_enemy_positions(): return set(positions[:4]) +def is_valid_position(pos): + return pos in range(1, 26, 1) + + def prompt_for_player_positions(): while True: raw_positions = input("WHAT ARE YOUR FOUR POSITIONS? ") positions = set(int(pos) for pos in raw_positions.split()) - # Verify user inputs (for example, if the player gives a # a position for 26, the enemy can never hit it) if (len(positions) != 4): print("PLEASE ENTER 4 UNIQUE POSITIONS\n") continue - elif (any(pos not in range(1, 26, 1) for pos in positions)): + elif (any(not is_valid_position(pos) for pos in positions)): print("ALL POSITIONS MUST RANGE (1-25)\n") continue else: return positions +def prompt_player_for_target(): + while True: + target = int(input("WHERE DO YOU WISH TO FIRE YOUR MISSLE? ")) + if not is_valid_position(target): + print("POSITIONS MUST RANGE (1-25)\n") + continue + + return target + + # Messages correspond to outposts remaining (3, 2, 1, 0) PLAYER_PROGRESS_MESSAGES = ( "YOU GOT ME, I'M GOING FAST. BUT I'LL GET YOU WHEN\n" @@ -83,7 +96,7 @@ def play(): print(enemy_positions) while True: - target = int(input("WHERE DO YOU WISH TO FIRE YOUR MISSLE? ")) + target = prompt_player_for_target() if target in enemy_positions: print("YOU GOT ONE OF MY OUTPOSTS!")