所以我创造了一个简单的寻宝游戏,你可以在一个棋盘上寻找三个宝藏。但在猜了6次之后,我陷入了困境!X代表你搜索过的区域,$符号是你找到的宝藏。请帮忙!!!!在import random

def hide_treasure(board):

treasures=0

while treasures<=3:

random_row=random.randrange(0,5)

random_col=random.randrange(0,5)

if(0<=random_row<5) and(0<=random_col<5) and (board[random_row] [random_col]==" "):

board[random_row][random_col]="T"

treasures+=1

def display_board(board,show_treasure=False):

for col in range(5):

print " %d " %col,

print

for row in range(5):

print " %d:" %(row)," | ".join(board[row]).replace("T"," ")

print " ---+---+---+---+---"

if show_treasure==True:

" ".replace(" ","T")

def make_user_move(board):

valid_move=False

while not valid_move:

try:

ask_row=input("What row would you like to search (0-4): ")

ask_col=input("What col would you like to search (0-4): ")

if board[ask_row][ask_col]=="T":

board[ask_row][ask_col]="$"

print

print"YES! You found a treasure."

return True

elif board[ask_row][ask_col]=="$" or board[ask_row][ask_col]=="X":

print

print"You already tried there, please pick again."

else:

board[ask_row][ask_col]="X"

print

print"Nothing there."

break

except ValueError:

print"Integers only for row and column values. Please try again!"

continue

except IndexError:

print

print"Sorry invalid location. Please try again!"

def main():

board=[[" "," "," "," "," "],[" "," "," "," "," "],[" ", " "," "," "," ",],[" "," "," "," "," "],[" "," "," "," "," "]]

print"WELCOME TO TREASURE HUNT!"

guess=10

treasures=0

while guess!=0 and treasures!=3:

print

print"You have",guess,"guesses left and have found",treasures,"/3 treasures"

hide_treasure(board)

display_board(board)

guess-=1

if make_user_move(board):

treasures+=1

if guess==0 and treasures!=3:

display_board(show_treasure=True)

print"OH NO! You only found %d"%treasures,"/3 treasures."

print

print"*** GAME OVER ***"

elif treasures==3:

display_board(board)

print"CONGRATULATIONS! You found ALL of the hidden treasure."

print

print"*** GAME OVER ***"

main()

python文字游戏循环3次_Python寻宝游戏中的无限循环相关推荐

  1. python getattribute方法_Python:避免getattribute中的无限循环__

    为了避免无限循环,需要仔细编写方法__getattribute__.例如:class A: def __init__(self): self.x = 100 def __getattribute__( ...

  2. vue本地没事放到服务器上无限循环,解决vue中的无限循环问题

    项目中遇到了这样一个问题:每一种产品有对应的服务费,每一个商家有多种商品要单独计算每一家的服务费,最后汇总总的服务费用.我直接写了一个方法来计算出每个商家和总的服务费用并return出来.如果不看控制 ...

  3. java中无限循环的方法_Java中的无限循环

    Java中的无限循环 在Java中查看下面的无限while循环. 它会导致它下面的语句编译时错误. while(true) { System.out.println("inside whil ...

  4. python随机出现外星人飞船_python外星人入侵游戏如何改变飞船的大小

    Python从入门到实践里的<外星人入侵> Python外星人入侵问题分享助世界上从来不缺少风景,缺少的只是欣赏的心情. Python项目外星人入侵添加爆照效果有没有原码<Pytho ...

  5. python种颜色循环_python – 重置Matplotlib中的颜色循环

    您可以使用Axes.set_color_cycle将颜色循环重置为原始颜色.查看此代码,可以执行实际工作: def set_color_cycle(self, clist=None): if clis ...

  6. python向excel写数据_Python向excel中写入数据的方法

    最近做了一项工作需要把处理的数据写入到Excel表格中进行保存,所以在此就简单介绍使用Python如何把数据保存到excel表格中. 数据导入之前需要安装 xlwt依赖包,安装的方法就很简单,直接 p ...

  7. python中for无限循环_关于循环:在Python中从1循环到无穷大

    在C语言中,我会这样做: 1 2 3 4int i; for (i = 0;; i++) if (thereIsAReasonToBreak(i)) break; 如何在Python中实现类似的功能? ...

  8. python中for无限循环_循环-在Python中从1循环到无穷大

    循环-在Python中从1循环到无穷大 在C语言中,我会这样做: int i; for (i = 0;; i++) if (thereIsAReasonToBreak(i)) break; 如何在Py ...

  9. 基于python脚本的参数化建模_Python在ABAQUS中

    Python在ABAQUS中 所属分类:其他 开发工具:Python 文件大小:17325KB 下载次数:0 上传日期:2020-11-25 11:47:17 上 传 者:qwe12334 说明:   ...

  10. pythonturtle循环语句_如何打破Turtle图形Python中的无限循环

    主要的问题是while循环持续了无限长的时间.在#This is infinite loop because 'side' iterator is ALWAYS in the sequence ret ...

最新文章

  1. php动态生成apk渠道包,Android自动生成渠道包
  2. bootstrap-反色导航条
  3. EM算法和GMM(下)
  4. 中lisp文件_关于 Emacs 中的变量你需要知道的事情 | Linux 中国
  5. IOT(29)---深入解析物联网操作系统(架构/功能/实例分析)
  6. Excel中的图表制作(一) -各种商品销售量显示
  7. CodeMix使用的语言和框架(一):JavaScript
  8. 5.软件架构设计:大型网站技术架构与业务架构融合之道 --- 网络
  9. C语言小项目——电子秒表(毫秒级)
  10. Centos 7下PCIe Bus Error: severity=Corrected, type=Data Link Layer解决方案
  11. Blazor预研与实战
  12. 如何使用TPTP中的IDatapool
  13. 生物信息学(3)——双序列比对之BLAST算法简介
  14. 中科曙光 量子计算机,中科曙光 量子计算机 量子计算机的曙光
  15. 【算法】牛顿迭代法求平方根的原理和误差分析
  16. python去除字符串中的空格、特殊字符、指定字符
  17. 笨方法学Python(1-5)
  18. 基于PHP的网上商城
  19. tomcatServlet
  20. DELPHI 多线程同步几种方法

热门文章

  1. mysql和jdbc(韩)
  2. Windows 8.1 安装Ruby on Rails手记
  3. Cloud E随笔-后端_piece3--实现登录功能
  4. RRD_rrd4j的使用说明
  5. drupal7 php版本,为内置PHP 5.4服务器提供Drupal 7
  6. 【Android】Instant Run原理解析
  7. 千千静听V5.6 Beta3 美化增强版
  8. 海外 Android 三方应用市场
  9. STM32F103C6T6初步学习
  10. 安装U8后服务器开机加载信息慢,用友U8 安装ADSL后,连接服务器非常慢