I'm just starting to learn python and have come across some trouble when trying to program a simple 1-D version of single player battleship.
2 things I can't seem to make work:
1 2 3 * 5
1 2 X * 5
from random import randint
board = ["O","O","O","O","O"]
print ("Let's play Battleship!")
attempts = 1
while attempts < 4:
guess = int(input("Guess Where My Ship Is: "))
if guess == ship:
print ("Congratulations Captain, you sunk my battleship!")
print ("You missed my battleship!")
print("Better luck next time, Captain!")
Good practice: set the board size to a variable so you can reference it regularly. Put this at the top
size = 5 # Can be changed later if you want to make the board bigger
Next, have your ship location be chosen based on that
ship = randint(0, size)
Instead of making a board filled with 0's, Generate the board dynamically so that it's already pre-populated with the possible values.
board =  # Creating an empty board for i in range(1, size): position = str(i) # Converting integers to strings board.append(position) # Adding those items to the board
Then, inside of the game logic, after the "You missed my battleship" line, change the relevant square on the board
... print("You missed my battleship!") number_guess = int(guess) - 1 # Because lists are zero-indexed board[number_guess] = "*" # Assign "*" to the spot that was guessed if attempts < 3: ...