python核心编程-函数参数传递
2015-12-01 00:07
411 查看
#!/usr/bin/env python # -*- coding: UTF-8 -*- from operator import add,sub from random import randint,choice ops = {'+':add,'-':sub} MAXTRIES = 2 def doprob(): op = choice('+-') nums = [randint(1,10) for i in range(2)] #在1-10中选择2个数 print 'nums[0]:%d,nums[1]:%d'% (nums[0],nums[1]) nums.sort(reverse=True) #连个数大小排序 ans=ops[op](*nums) #两个数的算术结果 print 'ans:%d'% ans pr='%d %s %d='%(nums[0],op,nums[1]) #输出类似:8+3= oops=0 while True: try: if int(raw_input(pr)) == ans: #如果输入正确 print 'correct' break if oops == MAXTRIES: #输入错误次数达到最大,给出正确答案 print 'answer\n%s%d'%(pr,ans) else: print 'incorrect ... try agin' oops += 1 except (KeyboardInterrupt,EOFError,ValueError): print 'invalid input ... try again' def main(): while True: doprob() try: opt = raw_input('Again? [y]').lower() if opt and opt[0] == 'n': break except (KeyboardInterrupt,EOFError): break if __name__ == '__main__': main()
D:\Python27\test>func3.py nums[0]:6,nums[1]:7 ans:1 7 - 6=2 incorrect ... try agin 7 - 6=1 correct Again? [y]y nums[0]:6,nums[1]:7 ans:13 7 + 6=13 correct Again? [y]n D:\Python27\test>
相关文章推荐
- python抓取网页中图片并保存到本地
- Python之路(一)
- python-32:再一次源码分析
- python-33:极视界爬虫V-0.2
- python-34:极视界爬虫总结
- python-35:urllib 和 urllib2 模块
- python-36:在下一个实例开始之前先闲聊几句
- python-37: 模拟登录
- python-38: 网页登陆流程
- python-39: cookies
- Python编码问题整理
- python杂记
- Eclipse+python开发环境配置pydev
- Python--访问限制
- Python语法背后的东西(解释器层面)
- python核心编程-函数调用
- Theano 学习笔记
- python3 mysql
- python核心编程-函数返回值
- python pip安装scrapy各种错误处理