摘要:本文主要向大家介绍了Python语言学习-游戏,通过具体的内容向大家展示,希望对大家学习Python语言有所帮助。
本文主要向大家介绍了Python语言学习-游戏,通过具体的内容向大家展示,希望对大家学习Python语言有所帮助。
# 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则循环开始
本文由职坐标整理并发布,希望对同学们学习Python有所帮助,更多内容请关注职坐标编程语言Python频道!
您输入的评论内容中包含违禁敏感词
我知道了
请输入正确的手机号码
请输入正确的验证码
您今天的短信下发次数太多了,明天再试试吧!
我们会在第一时间安排职业规划师联系您!
您也可以联系我们的职业规划师咨询:
版权所有 职坐标-一站式IT培训就业服务领导者 沪ICP备13042190号-4
上海海同信息科技有限公司 Copyright ©2015 www.zhizuobiao.com,All Rights Reserved.
沪公网安备 31011502005948号