What if the user doesn't enter any number and press just enter


#1

IF we just enter without any number, it throws back exception. How do we handle this ?


#2

You can inspect the string before converting it:

if user_input.isdigit():
    user_number = int(user_input)
else:
    print 'This is not an integer.'

or you can try to convert it and do something if there's an exception:

try:
    user_number = int(user_input)
except ValueError:
    print 'This is not an integer.'

You might want to put this in a loop which keeps going until valid input is entered, and you might also check that the number is within the range that you expect.


#3

I'm trying to figure this out using this code:

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)

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

    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 "Turn", turn + 1
    print_board(board)
    if turn == 9:
        print "Game Over"

I got the try one but not the first one with if.

for turn in range(10):
    try:
        guess_row = int(raw_input("Guess Row:"))
    except ValueError:
        print "You didn't make a guess" #'This is not an integer.'
    try:
        guess_col = int(raw_input("Guess Col:"))
    except ValueError:
        print "You didn't make a guess" #'This is not an integer.'
    
    guess_row = int(raw_input("Guess Row:"))
    guess_col = int(raw_input("Guess Col:"))

I will come back to this later because it doesn't loop?


#4

If you want something to repeat you'll have to add a loop yourself, doesn't happen on its own!

Also, you only need one try-except, like so:

try:
    guess_row = int(raw_input("Guess Row:"))
    guess_col = int(raw_input("Guess Col:"))
except ValueError:
    print "You didn't make a guess" #'This is not an integer.'

And then you'd put a loop around that, and break out once the input is valid


#5

Yes, I placed the question mark at the end for possibly a working code, because I run into problems with it. I was saying I would come back to this and fix it for myself later I suppose unless someone could possibly help me. Since the next Unit has loops, which I have yet to finish. I must see how it fits to understand it more and if I don't I will ask or look for the answer. Trial and error is default if I don't get feedback of how it is placed into the code we are told to use.

All I want is to know how it looks in code and for it to work and understand it. The struggle is real for something probably so simple.

Thank you!


#6

I'm really trying to figure this out but I feel like I'm at a halt until I get it and I'm just not figuring it out. It gives me different errors every time I change something.

I want the player to be able to press enter and for it tell them to guess again with an answer besides a blank one. Or where nothing works unless they enter an answer. Idk this is confusing. I tried many times.

If I put break after printing the string telling them to guess again it stops the code and I have to run it again.

I don't know what to do with a loop for it and how to place it with defining it?

I don't know how to do this? Help please?