ERROR If Blank Input Given


#1

If I tried to enter any value as blank, getting below error:

Guess Row: 2
Guess Col:

"Traceback (most recent call last):
File "python", line 30, in
ValueError: invalid literal for int() with base 10: ''

How to handle blank input??
Kindly help


#2

I have this question too? I tried using else print "empty" but that wont work.


#3

Maybe put an if statement in:
if len(guess_row) <= 0 or len(guess_col) <=0:
print "Empty"


#4

I have tried but it's not working.

The error occurring at the time of "int(raw_input ("user input value"))"..
here "user input value" is blank and its giving error while it typecasting into "int".


#5

Can you share your code?


#6

from random import randint

board = []

for x in range(5):
board.append(["O"] * 5)

def print_board(board):
for row in board:
print " ".join(row)

print "Let's play Battleship!"
print_board(board)

def random_row(board):
return randint(0, len(board) - 1)

def random_col(board):
return randint(0, len(board[0]) - 1)

ship_row = random_row(board)
ship_col = random_col(board)
print ship_row
print ship_col

Everything from here on should go in your for loop!

for turn in range(4):
guess_row = int(raw_input("Guess Row:"))
guess_col = int(raw_input("Guess Col:"))

NOT WORKING -- len() func does not work with int value.

if len(guess_row) <= 0 or len(guess_col) <=0:

print "Empty"

if guess_row == "" or guess_col == "":
    print "Enter both values"
else:
    if guess_row == ship_row and guess_col == ship_col:
        print "Congratulations! You sunk my battleship!"
        break
    else:
        if (guess_row < 0 or guess_row > 4) or (guess_col < 0 or guess_col > 4):
            print "Oops, that's not even in the ocean."
        elif(board[guess_row][guess_col] == "X"):
            print "You guessed that one already."
        else:
            print "You missed my battleship!"
            board[guess_row][guess_col] = "X"
    print_board(board)
print (turn + 1)

if turn == 3:
print "Game Over"


#7

basically it doesn't like the 'int' before the input. So you might have to get rid of that and then change it to an integer at a later point?


#8

for turn in range(4):
guess_row = (input("Guess Row:"))
guess_col = (input("Guess Col:"))
if (len(guess_row)) == 1 or (len(guess_col) == 1):
print("Empty")
guess_row = int(guess_row)
guess_col = int(guess_col)
if guess_row == ship_row and guess_col == ship_col:
print ("Congratulations! You sunk my battleship!")
break


#9

Thank you.. It's working fine now...

for turn in range(4):
guess_row = raw_input("Guess Row:")
guess_col = raw_input("Guess Col:")

if len(guess_row) <= 0 or len(guess_col) <= 0:
    print "Pls enter both values"
    print_board(board)
    print (turn + 1)
    continue

guess_row = int(guess_row)
guess_col = int(guess_col)


if guess_row == ship_row and guess_col == ship_col:
    print "Congratulations! You sunk my battleship!"
    break

#10

No problem. I had to take out the raw_input and just put input as I was working on actual python 3.5.0 which doesn't accept raw_input