After 'Extra Credit' - Migrating to Python 3.5


#1

If it's of any use, I migrated the completed course Battleship code into Python 3.5 and was met with several errors - mainly syntax issues. I edited them (after a long session of trial and error) in IDLE. I then added some Game Over features to polish it up a bit. I will continue to add some of the suggestions listed in the Extra Credit at some point - but so far, this is the result...

Battleship 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) - 1)

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

print ("")

The next two lines show where the ship is!

print (ship_row)

print (ship_col)

Everything from here on should go in your for loop!

Be sure to indent four spaces!

for turn in range(4):
print ("")
print (("Turn"), turn + 1)
guess_row = int(input("Guess Row: "))
guess_col = int(input("Guess Col: "))
if guess_row == ship_row and guess_col == ship_col:
print ("Congratulations! You sunk the 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 the Battleship!")
board[guess_row][guess_col] = "X"
if turn == (3):
print ("")
print_board(board)
print ("")
print ("Game Over!")
print ("")
print (("The Battleship was at...")\
+ ("Row ") + str(ship_row) + (" ")\
+ ("Col ") + str(ship_col))
board[ship_row][ship_col] = "B"
print ("")

print_board(board)

#2

That looks better! Doesn't it?


#3

Also, there are only a few differences between 2.7.* and 3.*.

 1. Print became a built-in function
 2. raw_input was depreciated replaced by input

These are the only things that will really change your code from codeacademy.

I can't remember any of the major differences that would make an impact, so best of luck.


#4

Yes - much bettter...! :smile:


#5

Took me ages to work out that raw_input was the problem and that input was the solution.


#6

There was also an issue with syntax requiring me to double-bracket some of the code.


#7

Post a link of your code in my MEGA THREAD for extra credit this counts.

EDIT: and a short description of what you did.

I am maintaining that post so we can give new programmers something to look at and learn from.

As always best of luck!


#8

I am new to Python (about a week or so) and to coding in general (though a few years ago, I did dabble with MS Small Basic for a while), so I found the project both challenging and rewarding in my own little way. Looking forward to moving on to the next stage now. :smile:


#9

Ok, thank you - will try that...!


#10

I'm already done with all this, but it helps me retain the knowledge I gained when i help others, also it helps

Keep at it, also don't let people tell you Python sucks. They just have a limited understanding of what it can and cannot do. I dabble in c++, c#, and a few others. I am working on fully bringing c++ under my belt though then java.

I have a ways to go but every step I take brings me closer.


#11

Nobody will put me off Python because I like it and I am learning it to use with a Pi, which I will get when I have a good knowledge of the language - just as a hobby. I have good reason to continue with it.


#12

Don't wait just get the pi, it is a GREAT learning tool, and the things that you can with it are exceptional.


#13

Ok, thanks - have been looking at some of the zillions of projects out there - will keep me busy into my retirement days (not that far off now)...!


#14

If you are into machinery I would recommend making a tabletop CNC and using it to cut word, thin/soft metals and foams some glass and Plexiglas.

You just need some stepper motors a pi and I forget the shield you need to control the motors and most people use a dremal motor at first for the spindle.

You can make all types of stuff from there from frames for thing you can easily convert it to a 3-d printer too.

Yea, pis are epic stuff.


#15

Sounds amazing. I am really looking forward to dabbling - slowly at first, obviously - but who knows where such a hobby can lead? One of the first projects I have in mind is to make a basic wifi tablet with the small keyboard monitor that is available for it - and to incorporate one of the sensor cards, just to see what can be achieved with it.


#16

Sounds nice, you can do things like an RIFD sensor that detects when you get near it. Like when you come near your door and it unlocks it. You can then add encryption to it so that it would be unable to be hacked easily.

It's not that hard to do that actually, just look up cyphers and the two-way handshake for mobile.

I would suggest per-built modules for most of it because there are really good ones out there.


#17

Nice one. Thanks! :smile: