Python Codecademy 9单元Battleship!
Posted 十月的雨
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Python Codecademy 9单元Battleship!相关的知识,希望对你有一定的参考价值。
from random import randint #引入随机函数 board = [] #建一个空的list for x in range(5): board.append(["O"] * 5) #用range()方法给list写值,每一个["0"]*5是item def print_board(board): for row in board: print " ".join(row) #输出list,去掉引号和括号,用空格连接 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) # 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: 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"): #如果已经被填充X说明是重复值 print "You guessed that one already." else: print "You missed my battleship!" board[guess_row][guess_col] = "X" #猜错的值用X填充 print (turn+1) if turn==3: print "Game Over" print_board(board)
codecademy第九单元的小游戏,虽然通过了,但是感觉还有硬伤,需要后期再调一下,例如轮次是从0开始的,输入的第几行第几列是不是应该-1,输入的数值没有判空
以上是关于Python Codecademy 9单元Battleship!的主要内容,如果未能解决你的问题,请参考以下文章
codecademy练习记录--Learn Python(70%)
Learning Python from Codecademy
python PygLatin:CodeCademy上的一个Python作业