Good day,
I have started playing around with python and after making some scripts (dice generate, madlibs, hangman) I am trying to a battleship script.
I realise this coding might not be the most efficient way to code, but my current goal is the get it working with the limited understanding I have.
For part of the script I have function to take care of guesses the player make.
def turn():
board = ['A', 'B', 'C']
guess = input('Enter the target coordinate: ')
try:
if guess[:1].upper() in board and int(guess[1:]) in range(1,11):
guess = guess.upper()
print('Targeting %s!' % (guess))
return guess
elif guess == 'quit': quit
else:
print('Please enter a proper coordinate using A thru J and 1 thru 10.')
turn()
except:
print('Please enter a proper coordinate using A thru J and 1 thru 10.')
turn()
print(turn())
If I enter 'a2' I get the following
Targeting A2!
A2
which matches what I expect
When I enter a wrong input 'bla' or 'a13' I get prompted for a new input. All is going as expect so far, but when I now enter a correct input 'a2' I get the following
Targeting A2!
None
I can't grasp why 'return guess' works properly when I enter a good input right away, but not when I entered a bad input first.
Hopefully one of you can clarify this for me.