算术小游戏(python版)
2010-09-10 10:19
239 查看
来自《核心编程第二版》
#!/usr/bin/env python #easyMath.py---it is a veryeasy children math game. """ """ #use add(),sub(),and random number. from operator import add,sub from random import randint,choice #operators are stored in the dict. ops = {'+':add,'-':sub} MAXTRIES = 2 def doprob(): #firstly,it must random choice an operator, #and two number to reverse. op = choice('+-') nums = [randint(1,10) for i in range(2)] nums.sort(reverse=True) #get the value of ans. ans = ops[op](*nums) pr='%d %s %d='%(nums[0],op,nums[1]) #set the time of oops is 3,and if you input 3 times #and the result is incorrect,it inputs the correct #answer. oops = 0 while True: try: if int(raw_input(pr)) == ans: #the anwer is correct print 'correct' break if oops == MAXTRIES: #times=3 and incorrect print 'answer/n%s%d'%(pr,ans) else: print 'incorrect... try again' 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()
相关文章推荐
- Python实现的简单算术游戏实例
- Python实现的简单算术游戏实例
- python -- numpy 基本数据类型,算术运算,组合,分割 函数
- 首个python程序,一个猜数字的小游戏 ^0^
- python小游戏之圣诞树
- 5.python 算术操作符逻辑操作符
- 使用Python写一个小游戏
- Python3.3.2 算术运算----方法重写
- 用Python写一个无界面的2048小游戏
- Python pygame 接球的小游戏
- Python入门-猜数小游戏演变
- 编程语言的学习 ------ python小游戏猜数字(继续优化)
- python中的算术操作符
- python3编程入门(1)-算术、字符串与变量
- python摇骰子猜大小的小游戏
- opencv python学习笔记(五)图像的算术与
- Python 项目实践一(外星人入侵小游戏)第二篇
- Python编写微信打飞机小游戏(八)
- Python编写微信打飞机小游戏(十一)
- 用Python实现一个小游戏——2048