python实现用户答题功能


Posted in Python onJanuary 17, 2018

python实战,用户答题分享给大家。

主要包含内容,文件的读取,更改,保存。不同文件夹引入模块。输入,输出操作。随机获取数据操作

随机生成算数表达式,用户输入答案,正确记录分数,错误返回0,并把用户分数记录到文本文件中,如用户名不存在着新建用户

myPythonFunction.py包含三个函数

#coding=utf-8 
from random import randint 
from os import remove,rename 
 
#function 输入用户名字,获得用户得分,返回得分或者-1 
def getUserScore(userName): 
 try: 
  f = open("userScores.txt","r") 
  msg = f.readline() 
  score=-1; 
  while len(msg): 
   msg = msg.strip('\n') 
   msgArr = msg.split(",") 
   if(msgArr[0]==userName): 
    score = msgArr[1] 
    break 
   msg = f.readline() 
  f.close() 
  return score 
 except IOError: 
  f=open("userScores.txt","w") 
  f.close()  
  return -1 
 
#function 更新或者保存用户名字,用户得分 
def updateUserPoints(userName,score): 
 temp = getUserScore(userName) 
 if(temp==-1): 
  f = open("userScores.txt","a") 
  msg = userName+","+str(score)+"\n" 
  f.write(msg) 
  f.close() 
 else: 
  temp = open("userScores.tmp","w") 
  f = open("userScores.txt","r") 
  msg = f.readline() 
  while len(msg): 
   msg = msg.strip('\n') 
   msgArr = msg.split(",") 
   if(msgArr[0]==userName): 
    msgArr[1] = str(score) 
   temp.write(msgArr[0]+","+msgArr[1]+"\n")  
   msg = f.readline() 
  f.close() 
  temp.close() 
  remove("userScores.txt") 
  rename("userScores.tmp","userScores.txt") 
 
#function 获取随机生成的数学表达式 ,返回字符串   
def getQuestionString(): 
 operandList = [] 
 operatorList = [] 
 operatorDict=("+","-","*","**") 
 questionString = '' 
 for i in range(5): 
  operandList.append(randint(1,9)) 
 for j in range(4): 
  operatorList.append(operatorDict[randint(0,3)]) 
 for k in range(4): 
  questionString += str(operandList[k])+operatorList[k] 
 questionString +=str(operandList[4]) 
 return questionString

mathGame.py作为主函数

# -*- coding:utf-8 -*- 
import sys 
if 'H:\\python\func' not in sys.path: 
 sys.path.append('H:\\python\\func') 
import myPythonFunction as myfunc 
 
 
print("请输入你的名字:") 
use = input() 
use=use.strip("\n") 
count = 0 
if(myfunc.getUserScore(use)==-1): 
 print("你是个新用户!") 
 myfunc.updateUserPoints(use,0) 
else: 
 count = int(myfunc.getUserScore(use)) 
 print("你当前分数为:",count) 
 
while(1): 
 questionString=myfunc.getQuestionString() 
 result = eval(questionString) 
 print("问题:",questionString.replace("**","^")) 
 print("请输入你的答案:") 
 userResult = input() 
 userResult = userResult.strip("\n") 
 flag = True 
 if(userResult.startswith("-")): 
  userResult = userResult[1:] 
  flag = False 
 while((not userResult.isdigit()) and userResult!="exit"): 
  print("请输入数字,你的答案:") 
  userResult = input() 
  userResult = userResult.strip("\n") 
  if(userResult.startswith("-")): 
   userResult = userResult[1:] 
   flag = False 
 if(not flag): 
   userResult = "-"+userResult 
 if(userResult==str(result)): 
  print(1) 
  count = count+1 
 elif("exit"==userResult): 
  break  
 else: 
  print(0) 
myfunc.updateUserPoints(use,count) 
print("你当前分数为:",count) 
print("谢谢进入,欢迎下次再来!")

python实现用户答题功能

文件目录结构,将myPythonFunction.py放到func文件夹中。userScores.txt存放用户名和相应的得分

python实现用户答题功能

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Python 相关文章推荐
Python实现同时兼容老版和新版Socket协议的一个简单WebSocket服务器
Jun 04 Python
Python中统计函数运行耗时的方法
May 05 Python
pycharm执行python时,填写参数的方法
Oct 29 Python
Python中字符串List按照长度排序
Jul 01 Python
阿里云ECS服务器部署django的方法
Aug 29 Python
django中瀑布流写法实例代码
Oct 14 Python
python线程信号量semaphore使用解析
Nov 30 Python
将python包发布到PyPI和制作whl文件方式
Dec 25 Python
在Python中利用pickle保存变量的实例
Dec 30 Python
解决Python pip 自动更新升级失败的问题
Feb 21 Python
python内置进制转换函数的操作
Jun 02 Python
python数据分析之单因素分析线性拟合及地理编码
Jun 25 Python
python编程培训 python培训靠谱吗
Jan 17 #Python
Python温度转换实例分析
Jan 17 #Python
python3.5+tesseract+adb实现西瓜视频或头脑王者辅助答题
Jan 17 #Python
python+matplotlib绘制3D条形图实例代码
Jan 17 #Python
《Python学习手册》学习总结
Jan 17 #Python
浅谈Python对内存的使用(深浅拷贝)
Jan 17 #Python
Python分支结构(switch)操作简介
Jan 17 #Python
You might like
如何使用PHP中的字符串函数
2006/11/24 PHP
PHP 简易输出CSV表格文件的方法详解
2013/06/20 PHP
解析php addslashes()与addclashes()函数的区别和比较
2013/06/24 PHP
跨浏览器PHP下载文件名中的中文乱码问题解决方法
2015/03/05 PHP
php中序列化与反序列化详解
2017/02/13 PHP
Laravel下生成验证码的类
2017/11/15 PHP
thinkphp5框架前后端分离项目实现分页功能的方法分析
2019/10/08 PHP
PHP Swoole异步Redis客户端实现方法示例
2019/10/24 PHP
jquery中ajax调用json数据的使用说明
2011/03/17 Javascript
jQuery中校验时间格式的正则表达式小结
2013/09/22 Javascript
javaScript的函数对象的声明详解
2015/02/06 Javascript
jQuery实现图片文字淡入淡出效果
2015/12/21 Javascript
jquery dataTable 获取某行数据
2017/05/05 jQuery
webpack热模块替换(HMR)/热更新的方法
2018/04/05 Javascript
微信小程序实现跑马灯效果完整代码(附效果图)
2018/05/30 Javascript
微信小程序踩坑记录之解决tabBar.list[3].selectedIconPath大小超过40kb
2018/07/04 Javascript
使用React手写一个对话框或模态框的方法示例
2019/04/25 Javascript
layui操作列按钮个数和文字颜色的判断实例
2019/09/11 Javascript
Vue的属性、方法、生命周期实例代码详解
2019/09/17 Javascript
layui实现图片虚拟路径上传,预览和删除的例子
2019/09/25 Javascript
node脚手架搭建服务器实现token验证的方法
2021/01/20 Javascript
Python实现在matplotlib中两个坐标轴之间画一条直线光标的方法
2015/05/20 Python
Python多线程原理与用法详解
2018/08/20 Python
python障碍式期权定价公式
2019/07/19 Python
ubuntu上安装python的实例方法
2019/09/30 Python
python中的RSA加密与解密实例解析
2019/11/18 Python
如何使用Python发送HTML格式的邮件
2020/02/11 Python
python pandas.DataFrame.loc函数使用详解
2020/03/26 Python
Python可以用来做什么
2020/11/23 Python
西班牙英格列斯百货法国官网:El Corte Inglés法国
2017/07/09 全球购物
职业生涯规划设计步骤
2014/01/12 职场文书
教师节促销方案
2014/03/22 职场文书
个人委托书范本汇总
2014/10/01 职场文书
2014年信息中心工作总结
2014/12/17 职场文书
党风廉正建设个人工作总结
2015/03/06 职场文书
VUE之图片Base64编码使用ElementUI组件上传
2022/04/09 Vue.js