webpy入门猜数字游戏,webpy入门猜数字,简单的猜数字游戏,输入数
webpy入门猜数字游戏,webpy入门猜数字,简单的猜数字游戏,输入数
简单的猜数字游戏,输入数字,大了小了,给你提示。基于 web.py 框架,边学习边做,纯粹练手,附件有源代码和数据库文件。
[Python]代码
#encoding=utf-8import webimport randomimport osimport saeimport sae.constweb.config.debug = Falseurls = ("/", "index", "/guess/","guess" )def notfound(): s=render.notfound() return web.notfound(s)app_root = os.path.dirname(__file__)app = web.application(urls, globals())app.notfound=notfounddb = web.database(dbn='mysql', host=sae.const.MYSQL_HOST,port=int(sae.const.MYSQL_PORT),user=sae.const.MYSQL_USER, pw=sae.const.MYSQL_PASS, db=sae.const.MYSQL_DB)store = web.session.DBStore(db, 'sessions')templates_root = os.path.join(app_root, 'templates')render = web.template.render(templates_root)session = web.session.Session(app, store, initializer={'count': 0,'answer':-1,'re':' ','start':0,'end':100})class index: def GET(self): page= render.index('数字猜猜猜') return pageclass guess: def GET(self): session.kill() result='开始猜数字吧' count=0 start=0 end=100 page=render.game(result,count,start,end) return page def POST(self): try: data=web.input() nume=data.num session.count += 1 a=session.answer num=int(nume) if a==-1: session.answer=random.randrange(100) a=session.answer if num>=int(session.end): session.re='超过范围了' session.count -= 1 elif num<=int(session.start): session.re='超过范围了' session.count -= 1 elif num<a: session.re='<img border=0 src="../static/smaller.png" />' session.start=num elif num>a: session.re='<img border=0 src="../static/biger.png" />' session.end=num else: session.re='<img border=0 src="../static/bingo.png" /><br/>答案是:'+str(a) db.insert('count',count=session.count) result=session.re count=session.count start=session.start end=session.end page=render.game(result,count,start,end) return page except: session.kill() result='输入有误,请重新开始吧' count=0 start=0 end=100 page=render.game(result,count,start,end) return pageapplication = sae.create_wsgi_app(app.wsgifunc())
相关内容
- Python 字符编码实例,python编码实例,[Python]代码#c
- windows平台下如何用Python杀进程,平台python,[Python]代码
- Python脚本实现查找文件,,[Python]代码#c
- Python uuid模块,pythonuuid模块,python模块之uui
- 理解Python元类(Metaclasses),元类metaclasses,“元类的魔
- Python数组功能特点分析,python数组功能特点,实际上Py
- python 使用poplib pop3协议收取邮件,pythonpoplib,[Python]代码
- c,c++,java,python,ruby实现将文件读入数组的,pythonruby,c实现
- python从文本文件中选出使用频率最多的单词,,[Python]代
- python修改Windows注册表切换ie代理,pythonie,[Python]代码#
评论关闭