Python Programming
Lab 14
Python programming lab14
Battleship Game
use our knowledge of
lists, conditionals and functions
in Python
to build this Game
Python programming lab14
Python programming lab14
Python programming lab14
Python programming lab14
Python programming lab14
Python programming lab14
Python programming lab14
Python programming lab14
Python programming lab14
Python programming lab14
Python programming lab14
Python programming lab14
Python programming lab14
Python programming lab14
Python programming lab14
Python programming lab14
Python programming lab14
Python programming lab14
Python programming lab14
Python programming lab14
Python programming lab14
Python programming lab14
Python programming lab14
Python programming lab14
Python programming lab14
Python programming lab14
Python programming lab14
Python programming lab14
Python programming lab14
Python programming lab14
Python programming lab14
Python programming lab14
Python programming lab14
Python programming lab14
Python programming lab14
Python programming lab14
Python programming lab14
Python programming lab14
Python programming lab14
Python programming lab14
Python programming lab14
Python programming lab14
Python programming lab14
Python programming lab14
Python programming lab14
Python programming lab14
Python programming lab14
Python programming lab14
Python programming lab14
Python programming lab14
Python programming lab14
Python programming lab14
Python programming lab14
Python programming lab14
Python programming lab14
Python programming lab14
Python programming lab14
Game 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)
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):
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:
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 + 1)
print_board(board)
if turn == 3:
print "Game Over"
Python programming lab14
Python programming lab14
Python programming lab14
Python programming lab14
Python programming lab14

More Related Content

PDF
re3 - modern regex syntax with a focus on adoption
PDF
Programação Assíncrona com Kotlin Coroutines
PPTX
Handling character display in graphics mode using bios
PPTX
Coordenadas jugador
PPTX
[Pl in c++] 10. battleship game 5
PPTX
Battleship pp
PPTX
Battleship Marketing
PPTX
Ruby object model - Understanding of object play role for ruby
re3 - modern regex syntax with a focus on adoption
Programação Assíncrona com Kotlin Coroutines
Handling character display in graphics mode using bios
Coordenadas jugador
[Pl in c++] 10. battleship game 5
Battleship pp
Battleship Marketing
Ruby object model - Understanding of object play role for ruby

Viewers also liked (18)

PDF
Ruby Programming Introduction
PDF
Introduction to Game Programming: Using C# and Unity 3D - Chapter 6 (Preview)
PPT
Battleship game
PPT
Ruby For Java Programmers
PPTX
Battleship
PDF
Rapid Game Development with RUby and Gosu – Ruby Manor 4
PDF
Ruby for Java Developers
PDF
Ruby vs Java
KEY
Introducing Ruby
PPT
Ruby Basics
 
PPTX
Ruby Programming Language - Introduction
PDF
Ruby on Rails Presentation
PDF
Ruby everywhere
PDF
Ruby on Rails for beginners
PPT
A Difícil Arte de Conviver
PDF
Data analysis using spss
PPTX
Ruby language overview
PPTX
Ruby basics
Ruby Programming Introduction
Introduction to Game Programming: Using C# and Unity 3D - Chapter 6 (Preview)
Battleship game
Ruby For Java Programmers
Battleship
Rapid Game Development with RUby and Gosu – Ruby Manor 4
Ruby for Java Developers
Ruby vs Java
Introducing Ruby
Ruby Basics
 
Ruby Programming Language - Introduction
Ruby on Rails Presentation
Ruby everywhere
Ruby on Rails for beginners
A Difícil Arte de Conviver
Data analysis using spss
Ruby language overview
Ruby basics
Ad

Similar to Python programming lab14 (20)

DOCX
Artificial intelligence - python
PDF
PDF
Music as data
PDF
How to avoid Go gotchas - Ivan Daniluk - Codemotion Milan 2016
PDF
Python
PDF
Write a program in java in which you will build the“Sink theShipsGam.pdf
PPT
Programming simple games with a raspberry pi and
PDF
Introduction to Nim
PDF
A Taste of Python - Devdays Toronto 2009
KEY
Five Languages in a Moment
PDF
Are we ready to Go?
PPTX
Class 12 , computer 2 Python loop if-1.pptx
PDF
Rust Mozlando Tutorial
PDF
Hello! I'm trying to generate a battleship like board game with 10 row.pdf
PPTX
PYTHON PROGRAMMING INTERVIEW QUESTIONS.pptx
DOCX
week4_python.docx
PDF
Hammurabi
TXT
Assignment6
PDF
The Magnificent Seven
PPT
04 Console input output-
Artificial intelligence - python
Music as data
How to avoid Go gotchas - Ivan Daniluk - Codemotion Milan 2016
Python
Write a program in java in which you will build the“Sink theShipsGam.pdf
Programming simple games with a raspberry pi and
Introduction to Nim
A Taste of Python - Devdays Toronto 2009
Five Languages in a Moment
Are we ready to Go?
Class 12 , computer 2 Python loop if-1.pptx
Rust Mozlando Tutorial
Hello! I'm trying to generate a battleship like board game with 10 row.pdf
PYTHON PROGRAMMING INTERVIEW QUESTIONS.pptx
week4_python.docx
Hammurabi
Assignment6
The Magnificent Seven
04 Console input output-
Ad

More from profbnk (20)

PPTX
JavaServer Pages
PPTX
Java swing
PPTX
Java Virtual Machine
PPTX
Big data2.0.040915
PDF
Big data
PPTX
Python programming advance lab api we pay
PPTX
Python programming advance lab api how to
PPTX
Python programming advance lab api npr 2
PPTX
Python programming lab 20
PPTX
Python programming lab 19
PPTX
Python programming lab 18
PPTX
Python programming lab 23
PPTX
Python programming21
PPTX
Python programming lab 17
PPTX
Python programming lab16
PPTX
Python programming lab15
PPTX
Python programming lab13
PPTX
Python programming lab12
PPTX
Python programming lab 11
PPTX
Python programming lab 10
JavaServer Pages
Java swing
Java Virtual Machine
Big data2.0.040915
Big data
Python programming advance lab api we pay
Python programming advance lab api how to
Python programming advance lab api npr 2
Python programming lab 20
Python programming lab 19
Python programming lab 18
Python programming lab 23
Python programming21
Python programming lab 17
Python programming lab16
Python programming lab15
Python programming lab13
Python programming lab12
Python programming lab 11
Python programming lab 10

Recently uploaded (20)

PDF
GSA-Past-Papers-2010-2024-2.pdf CSS examination
PPSX
namma_kalvi_12th_botany_chapter_9_ppt.ppsx
PDF
Developing speaking skill_learning_mater.pdf
PDF
LATAM’s Top EdTech Innovators Transforming Learning in 2025.pdf
PPTX
principlesofmanagementsem1slides-131211060335-phpapp01 (1).ppt
PDF
Design and Evaluation of a Inonotus obliquus-AgNP-Maltodextrin Delivery Syste...
PDF
GIÁO ÁN TIẾNG ANH 7 GLOBAL SUCCESS (CẢ NĂM) THEO CÔNG VĂN 5512 (2 CỘT) NĂM HỌ...
PDF
IS1343_2012...........................pdf
PDF
African Communication Research: A review
PPTX
GW4 BioMed Candidate Support Webinar 2025
PPTX
ENGlishGrade8_Quarter2_WEEK1_LESSON1.pptx
PPTX
Neurological complocations of systemic disease
PDF
Chevening Scholarship Application and Interview Preparation Guide
PPTX
Cite It Right: A Compact Illustration of APA 7th Edition.pptx
PDF
HSE 2022-2023.pdf الصحه والسلامه هندسه نفط
PPTX
pharmaceutics-1unit-1-221214121936-550b56aa.pptx
PDF
Kalaari-SaaS-Founder-Playbook-2024-Edition-.pdf
PPTX
Unit1_Kumod_deeplearning.pptx DEEP LEARNING
PDF
BSc-Zoology-02Sem-DrVijay-Comparative anatomy of vertebrates.pdf
PDF
Global strategy and action plan on oral health 2023 - 2030.pdf
GSA-Past-Papers-2010-2024-2.pdf CSS examination
namma_kalvi_12th_botany_chapter_9_ppt.ppsx
Developing speaking skill_learning_mater.pdf
LATAM’s Top EdTech Innovators Transforming Learning in 2025.pdf
principlesofmanagementsem1slides-131211060335-phpapp01 (1).ppt
Design and Evaluation of a Inonotus obliquus-AgNP-Maltodextrin Delivery Syste...
GIÁO ÁN TIẾNG ANH 7 GLOBAL SUCCESS (CẢ NĂM) THEO CÔNG VĂN 5512 (2 CỘT) NĂM HỌ...
IS1343_2012...........................pdf
African Communication Research: A review
GW4 BioMed Candidate Support Webinar 2025
ENGlishGrade8_Quarter2_WEEK1_LESSON1.pptx
Neurological complocations of systemic disease
Chevening Scholarship Application and Interview Preparation Guide
Cite It Right: A Compact Illustration of APA 7th Edition.pptx
HSE 2022-2023.pdf الصحه والسلامه هندسه نفط
pharmaceutics-1unit-1-221214121936-550b56aa.pptx
Kalaari-SaaS-Founder-Playbook-2024-Edition-.pdf
Unit1_Kumod_deeplearning.pptx DEEP LEARNING
BSc-Zoology-02Sem-DrVijay-Comparative anatomy of vertebrates.pdf
Global strategy and action plan on oral health 2023 - 2030.pdf

Python programming lab14

  • 3. Battleship Game use our knowledge of lists, conditionals and functions in Python to build this Game
  • 61. Game 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)
  • 62. 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! # Be sure to indent four spaces! for turn in range(4): 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:
  • 63. 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 + 1) print_board(board) if turn == 3: print "Game Over"