#!/usr/bin/env python

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) ]
nums.sort(reverse=True)
ans = ops[op](*nums)
pr = '%d %s %s = ' % (nums[0], op, nums[1])
oops = 0
while True:
try:
if int(raw_input(pr)) == ans:##只有正确作答才跳出循环
print 'correct'
break
if oops == MAXTRIES:##第二次给出正确答案,但是依然是这道题目
print 'sorry... the answer is\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()

最新文章

  1. MSSQLSERVER执行计划详解
  2. CentOS 6.5 Python Image Library 配置
  3. LintCode Search Insert Position
  4. java设计之简单的JAVA计算器
  5. ui的设计原则
  6. js调用ASP.NET打印代码
  7. B - 楼下水题(扩展欧几里德)
  8. txt文件的读取
  9. 网页设计——4.html基本标签链接,图片,表格
  10. 简述“类(class)”,“类库(class library)”,“包(package)”,“jar文件”这四个概念间的关系
  11. JavaIO流——简单对文件的写入及读取(二)
  12. Centos7 安装PhantomJS
  13. CF438E The Child and Binary Tree
  14. [No0000120]Python教程3/9-第一个Python程序
  15. information_schema系列九
  16. 温故而知新 Vue 原来也有this.$forceUpdate();
  17. Spring Security构建Rest服务-1000-使用SpringSocial开发第三方登录之大白话OAuth协议
  18. 2017-2018-1 20155232 《信息安全系统设计基础》第十周课堂测试(ch06)补交
  19. Siki_Unity_3-3_背包系统
  20. python基础——操作系统简介

热门文章

  1. ShardedJedis的分片原理
  2. 如何用Curl 来post xml 数据
  3. A*算法 寻路
  4. 最全的CSS浏览器兼容问题【FF与IE】
  5. 51nod1482
  6. Delphi中的文件扩展名
  7. JQ插件写法 扩展JQ方法
  8. L1-035 情人节
  9. NSScanner
  10. TreeMap源码学习