[Tutor] Battleships - Error with blank input
Alan Gauld
alan.gauld at yahoo.co.uk
Thu May 4 05:33:47 EDT 2023
On 03/05/2023 23:09, Sharj Ahmed wrote:
> I've written the below code, but need to add some validation in for when
> the user inputs nothing.
Your code has lost all indentation which is of course critical
in Python. You need to resend the message using *plain text*
format not HTML.
> The blank input for rows and columns causes the application to crash.
Please always include the error messages - I'm assuming you
get an error message?! You should do.
I'll try to find the input statements...
> *def fire_shot():"""Asking player what row and column to fire their
> shot"""
row = input("\nEnter ship row 1-8: ")while row not in "12345678":
If row is an empty string this will pass because the empty string is in
every string.
>>> "" in "123"
True
>>> "" in ""
True
You will need to explicitly check for an empty string:
while row and row not in "12345678":
> The error message that appears is:
> [image: image.png]
You need to cut 'n paste the text. The server strips binary
attachments as security hazards.
--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos
More information about the Tutor
mailing list