出于某种原因,我用来检查猜测位置和实际位置是否相同的if语句就是不起作用。我已经安排好了,所以我知道船在哪里。我试着让它打印出所有的变量和它们各自的类型,就在我输入猜测之后,在它检查它们是否相等之前,但是由于某种原因,它继续显示我错过了。在

但不管怎样,它在这里:from random import randint

board = []

for x in range(0, 5):

board.append(["O"] * 5)

def print_board(board):

for row in board:

print " ".join(row)

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

##########################################

#Value error checking and guess row inputs

##########################################

def get_guess_row():

guess_row = int(raw_input("Guess Row:"))

return guess_row

def value_guess_row():

try:

y = get_guess_row()

except ValueError:

print ("Please enter a valid number.")

y = value_guess_row()

return y

##########################################

#Value error checking and guess col inputs

##########################################

def get_guess_col():

guess_col = int(raw_input("Guess Col:"))

return guess_col

def value_guess_col():

try:

x = get_guess_col()

except ValueError:

print ("Please enter a valid number.")

x = value_guess_col()

return x

###############################

#Get the coloumn and row values

###############################

guess_col = value_guess_col()

guess_row = value_guess_row()

guess_col -= 1

guess_row -= 1

print ("Guessed coloumn: ", guess_col, "the type is: ", (type(guess_col)))

print ("Guessed row: ", guess_row, "the type is: ", (type(guess_row)))

print ("Actual coloumn: ", ship_row, "the type is: ", (type(ship_row)))

print ("Actual row: ", ship_col, "the type is: ", (type(ship_col)))

# Write your code below!

if guess_row == ship_row and guess_col == ship_col:

print ("Congratulations! You sank my battleship!")

elif guess_row not in range(5) or \

guess_col not in range(5):

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_board(board)

python 战舰_战舰python代码学院相关推荐

  1. 第一章 第一节:Python基础_认识Python

    Python基础入门(全套保姆级教程) 第一章 第一节:Python基础_认识Python 1. 什么是编程 通俗易懂,编程就是用代码编写程序,编写程序有很多种办法,像c语言,javaPython语言 ...

  2. 动态照片墙 python 实现_利用python生成照片墙的示例代码

    这篇文章主要介绍了利用python生成照片墙的示例代码,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧 PIL(Python Im ...

  3. python范例_最佳Python代码范例

    python范例 Python is a general purpose programming language which is dynamically typed, interpreted, a ...

  4. python实验原理_【python】《统计学原理实验教程(Python)》书中代码实现

    代码已经上传至GitHub <统计学原理实验教程(Python)>书中代码实现 简介 <统计学原理实验教程(Python)>是厦门大学出版社在2019年出版的图书,主要通过py ...

  5. matlab代码用python替换_用python替换Matlab

    我是一名工程专业的学生,我必须做大量的数值处理,绘图,模拟等工作.我目前使用的工具是Matlab. 我在大学计算机上使用它来完成大部分任务. 但是,我想知道有哪些可用的免费选项. 我已经做过一些研究, ...

  6. java python算法_用Python,Java和C ++示例解释的排序算法

    java python算法 什么是排序算法? (What is a Sorting Algorithm?) Sorting algorithms are a set of instructions t ...

  7. excel python插件_利用 Python 插件 xlwings 读写 Excel

    Python 通过 xlwings 读取 Excel 数据 去年底公司让我做设备管理,多次委婉拒绝,最终还是做了.其实我比较喜欢技术.做管理后发现现场没有停机率统计,而原始数据有,每次要自己在Exce ...

  8. python字符串_(Python基础教程之七)Python字符串操作

    Python基础教程 在SublimeEditor中配置Python环境 Python代码中添加注释 Python中的变量的使用 Python中的数据类型 Python中的关键字 Python字符串操 ...

  9. python 字符识别_使用python进行光学字符识别入门

    python 字符识别 语言模型设计 (Language Model Designing) Optical Character Recognition is the conversion of 2-D ...

  10. python扫雷_【Python】扫雷小游戏(PyQt5)

    [Python] 纯文本查看 复制代码#coding: utf-8 __author__ = "小冰|lovingxiaobing" __email__ = "86574 ...

最新文章

  1. oracle分页的使用,oracle中分页的实现方式.rownum的使用
  2. Apache Unable to find the wrapper https - did you forget to enable it when you configured PHP?
  3. map中的迭代删除操作注意问题
  4. Boost多线程-替换MFC线程
  5. Docker(五)如何构建Dockerfile
  6. 统计方形++(洛谷P2241题题解,Java语言描述)
  7. USACO(含training section)水题合集[5/未完待续]
  8. 网络学习(三)安装VMware Workstation 7
  9. [python] 当前时间输出字符串
  10. vmware 安装及使用虚拟机注意事项
  11. 第三讲 对话框的创建
  12. JarvisOJ Misc shell流量分析
  13. linux连接数问题
  14. win10 插入鼠标自动禁用触摸板的方法
  15. 计算所汉语词性标记集
  16. msgbox.html5.qq.com,怎样制作qq透明背景皮肤
  17. 第35讲:Xposed+模拟器的详细使用
  18. 通俗易懂、细致入微讲解卡尔曼滤波
  19. 转 TensorFlow Object Detection API 多GPU 卡平行计算,加速模型训练速度教学
  20. n个节点互异的拉格朗日插值基函数之和等于一证明

热门文章

  1. 困住黑猫 html5,百变大侦探白狐凶手是谁 百变大侦探白狐密室密码
  2. 2020李宏毅学习笔记——15.Recurrent Netural Network 上
  3. 论文笔记二 Positive, Negative and Neutral: Modeling Implicit Feedback inSession-based News Recommendatio。
  4. Windows下vscode实现pretty-print
  5. POI 操作word
  6. 经验分享:兢兢业业工作5年的我不敌半年新人,原来“迪赛智慧数”才是升职加薪的关键!
  7. 爱快路由器 默认ssh 管理用户名 sshd
  8. 苹果开发者账号开启双重认证教程
  9. 干货|以产品要素设计解读线上小微信贷
  10. 全志A40i移植 RTL8188FTV/RTL8188FU USB-WiFi