Advertisement
Guest User

1D Battleship

a guest
Dec 7th, 2019
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.02 KB | None | 0 0
  1. #!/usr/bin/env python3
  2.  
  3. from random import uniform as rand
  4.  
  5. def play():
  6.     max_shots = 7
  7.     target = int(rand(0, 10))
  8.     field = ['*']*10
  9.     found = False
  10.  
  11.     for i in range(max_shots):
  12.         print(f"You have {max_shots-i} shots left")
  13.         # print numbers
  14.         for i in range(len(field)):
  15.             print(f"{i} ", end='')
  16.         print('')
  17.  
  18.         # print status
  19.         for i in range(len(field)):
  20.             print(f"{field[i]} ", end='')
  21.         print('')
  22.  
  23.         # loop to ignore invalid inputs
  24.         while True:
  25.             # let the player guess
  26.             guess = int(input("Your try: "))
  27.  
  28.             if guess < 10 and guess >= 0:
  29.                 break
  30.  
  31.             print("invalid input")
  32.  
  33.         # guessed right
  34.         if guess == target:
  35.             print("You found the battleship!")
  36.             found = True
  37.             break
  38.  
  39.         # mark as tried
  40.         field[guess] = 'x'
  41.  
  42.     if not found:
  43.         print("Game over!")
  44.  
  45. if __name__ == "__main__":
  46.     play()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement