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