# coding:utf-8 #!/usr/bin/python ''' Created on Jan 4, 2018 @author: wayne ''' import random #导入模块 import time def displayIntro(): #定义一个函数,不执行代码,在调用的时候才执行 print('you are in a land full of dragons.In front of you') print('you see two caves.In one cave,the dragon is friendly') print('and will share his treasure with you the other dragon') print('is greedy and hungry,and will eat you on sight.') print() def chooseCave(): #定义一个玩家选择要进入的1或者2的函数 cave = '' #建立一个新的空字符串 while cave != '1' and cave != '2': ''' 布尔操做符,and,or,not and 表示:表达式全为true为true,其中一个值为False或者全为False 表达式结果为False >>> True and True True >>>True and False False >>> False and False False or:只要其中一个值为True 那么结果就为True ,只有两个值都为False的时候才是False >>>True or False True >>> True or True True >>>False or False False not :只能做用一个值,不能两个值组合在一块儿,not是取值的反值 >>> not True False >>> not False True >>> not (1 ==2) True ''' print('which cave will you go into?(1 or 2)') cave = input() #接收输入值, return cave def checkCave(chosenCave): #继承chosenCave的函数值 print('you approach the cave .....') time.sleep(2) print('it is dark and spooky.....') time.sleep(2) print('A large dragon jumps out in front of you!he opens his jaws and .....') print() time.sleep(2) friendlyCave = random.randint(1,2) #随机产生一个数字,在1,2中间 if chosenCave == str(friendlyCave): #判断输入值和随机产生的值是否一致,一致则玩家获胜,不然失败 print('Gives you his treasure!') else: print('Gobbles you down in one bite!') playAgain = 'yes' #存储一个初始值,"yes" while playAgain == 'yes' or playAgain == 'y': #判断条件是否为True displayIntro() #调用函数 caveNumber = chooseCave() #调用输入值的函数 checkCave(caveNumber) #调用checkCave的函数 print('Do you want to play again!(yes or no)') #判断玩家是否还要玩 playAgain = input() #接收键盘输入的值,若是是True则循环开始