Whats wrong with this code? - IN PYTHON 3.5


#1



Traceback (most recent call last):
File "C:\Users\Unipig\Music\School\Battleship.py", line 27, in
ship_row = type_row(board)
File "C:\Users\Unipig\Music\School\Battleship.py", line 14, in type_row
board = int(raw_input("Type in the number of row(between 0 and 4): "))
NameError: name 'raw_input' is not defined

Im using Python/IDLE 3.5 (32-bit), I think its something with me using Python 3 instead of Python 2 :confused:


I expected it to work normally


board = []

for i 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 type_row(board):
    board = int(raw_input("Type in the number of row(between 0 and 4): "))
    if board < 0 or board > 4:
        print ("Invalid number of row.")
        type_row(board)
    return board

def type_col(board):
    board = int(raw_input("Type in the number of column(between 0 and 4): "))
    if board < 0 or board > 4:
        print ("Invalid number of column.")
        type_col(board)
    return board

ship_row = type_row(board)
ship_col = type_col(board)

for turn in range(5):
    guess_row = int(raw_input("Guess Row:"))
    guess_col = int(raw_input("Guess Column:"))

    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, "is over.")
    print_board(board)
    if turn == 4:
        print ("Game over, the battleship lies at (", ship_row, ",", ship_col, ".")


#2

This python program was written in Python 2 but if you are running it on the Python 3 IDE they're two significant changes you should be aware of,

1. the print statement now requires parentheses, print (example)

2. raw_input is now just input()


#3

I did that, its the error output that is confusing me


#4

What is the error you are getting :slight_smile:


#5

Traceback (most recent call last):
File "C:\Users\Unipig\Music\School\Battleship.py", line 27, in
ship_row = type_row(board)
File "C:\Users\Unipig\Music\School\Battleship.py", line 14, in type_row
board = int(raw_input("Type in the number of row(between 0 and 4): "))
NameError: name 'raw_input' is not defined


#6

You still have a reference to raw_input and as I have jsut said in Python 3 raw_input is now written as just input()


#7

I just fixed it and the same error comes up :confused:


#8

Please post your updated code :slight_smile:


#9

Fixed it. I did not fix ALL the inputs.

Also how would I make it so once player 1 has typed in the co-ords of his battle ship, it then disappears when you hit enter on 'Type in the number of collum(between 0 and 4):' ?


#10

Hello? :confused:
Please reply, Im really stuck xD


#11

I asked for your updated code :slight_smile:


#12

board = []

for i 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 type_row(board):
board = int(input("Type in the number of row(between 0 and 4): "))
if board < 0 or board > 4:
print ("Invalid number of row.")
type_row(board)
return board

def type_col(board):
board = int(input("Type in the number of column(between 0 and 4): "))
if board < 0 or board > 4:
print ("Invalid number of column.")
type_col(board)
return board

ship_row = type_row(board)
ship_col = type_col(board)

for turn in range(5):
guess_row = int(input("Guess Row:"))
guess_col = int(input("Guess Column:"))

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, "is over.")
print_board(board)
if turn == 4:
    print ("Game over, the battleship lies at (", ship_row, ",", ship_col, ".")

:smiley:


#13

board = []

for i 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 type_row(board):
board = int(input("Type in the number of row(between 0 and 4): "))
if board < 0 or board > 4:
print ("Invalid number of row.")
type_row(board)
return board

def type_col(board):
board = int(input("Type in the number of column(between 0 and 4): "))
if board < 0 or board > 4:
print ("Invalid number of column.")
type_col(board)
return board

ship_row = type_row(board)
ship_col = type_col(board)

for turn in range(5):
guess_row = int(input("Guess Row:"))
guess_col = int(input("Guess Column:"))

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, "is over.")
print_board(board)
if turn == 4:
    print ("Game over, the battleship lies at (", ship_row, ",", ship_col, ".")

#14

What error are you getting now?


#15

Nothing, Im just wondering how you would make it so when player1 has set the ship's co-ords it will be hidden from player2 (removed from the console/blocked out)


#16

You cannot do this in Python IDLE because this would mean you are hiding one users input from another user. This is a method called Encapsulation and because I see you have an interest in game development I suggest you learn the Java programming language where Encapsulation is a fundamental and key topic.

But, if you would still like to try it out in Python you can do it this way I will write it in pseudocode,

ask player 1 for ship_col input
but do not update the player 1's board
ask player 2 for input 
but do not update player 2'sboard

#17

Could I make it so it prints a giant amount of spaces? Would that work?


#18

That is one way to do it but it would not look very pretty and a good game caters for UI (User Interface) and UX (User Experience)


#19

@cyandeathreaper ,

The console is a relatively unsophisticated interface, and there is no way to make the value that a user entered completely disappear automatically. However you can force a previous entry to scroll off the screen, for example ...

choice = int(input("\n" * 20 + "Enter row: "))

When the prompt appears, the previous entry will still exist, but will not be visible unless the user scrolls back up to look at it.


#20

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.