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使用QQ邮箱发送Email的方法实例
Feb 09 Python
Python实现破解猜数游戏算法示例
Sep 25 Python
Python3.遍历某文件夹提取特定文件名的实例
Apr 26 Python
基于Python实现定时自动给微信好友发送天气预报
Oct 25 Python
Python3实现爬虫爬取赶集网列表功能【基于request和BeautifulSoup模块】
Dec 05 Python
DataFrame.to_excel多次写入不同Sheet的实例
Dec 02 Python
Python键鼠操作自动化库PyAutoGUI简介(小结)
May 17 Python
什么是python的必选参数
Jun 21 Python
PyCharm2020.1.2社区版安装,配置及使用教程详解(Windows)
Aug 07 Python
python中reload重载实例用法
Dec 15 Python
Python Selenium破解滑块验证码最新版(GEETEST95%以上通过率)
Jan 29 Python
python可视化分析的实现(matplotlib、seaborn、ggplot2)
Feb 03 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
层叠菜单的动态生成
2006/10/09 PHP
PHP中英混合字符串截取函数代码
2011/07/17 PHP
destoon会员注册提示“数据校验失败(2)”解决方法
2014/06/21 PHP
php去掉URL网址中带有PHPSESSID的配置方法
2014/07/08 PHP
Yii rules常用规则示例
2016/03/15 PHP
jquery向.ashx文件post中文乱码问题的解决方法
2011/03/28 Javascript
Firefox/Chrome/Safari的中可直接使用$/$$函数进行调试
2012/02/13 Javascript
Javascript实现视频轮播在pc端与移动端均可
2013/09/29 Javascript
jQuery替换字符串(实例代码)
2013/11/13 Javascript
AngularJS中使用HTML5手机摄像头拍照
2016/02/22 Javascript
Jquery实现简单的轮播效果(代码管用)
2016/03/14 Javascript
JavaScript从数组的indexOf()深入之Object的Property机制
2016/05/11 Javascript
js判断是否为空和typeof的用法(详解)
2016/10/07 Javascript
javascript编程实现栈的方法详解【经典数据结构】
2017/04/11 Javascript
Angularjs 与 bower安装和使用详解
2017/05/11 Javascript
Angular2开发环境搭建教程之VS Code
2017/12/15 Javascript
jQuery length 和 size()区别总结
2018/04/26 jQuery
element-ui upload组件多文件上传的示例代码
2018/10/17 Javascript
在layui下对元素进行事件绑定的实例
2019/09/06 Javascript
浅析vue-cli3配置webpack-bundle-analyzer插件【推荐】
2019/10/23 Javascript
JS removeAttribute()方法实现删除元素的某个属性
2021/01/11 Javascript
[51:06]DOTA2-DPC中国联赛 正赛 Elephant vs Aster BO3 第二场 1月26日
2021/03/11 DOTA
Python的爬虫框架scrapy用21行代码写一个爬虫
2017/04/24 Python
python之Character string(实例讲解)
2017/09/25 Python
python中使用PIL制作并验证图片验证码
2018/03/15 Python
Python解决走迷宫问题算法示例
2018/07/27 Python
Python做智能家居温湿度报警系统
2018/09/25 Python
python文件处理fileinput使用方法详解
2020/01/02 Python
中文专业毕业生自荐信
2014/05/24 职场文书
施工工地安全标语
2014/06/07 职场文书
企业整改报告范文
2014/11/08 职场文书
生产设备维护保养制度
2015/08/06 职场文书
2016年离婚协议书范文
2016/03/18 职场文书
关于感恩的作文
2019/08/26 职场文书
uniapp开发小程序的经验总结
2021/04/08 Javascript
python实现简单的名片管理系统
2021/04/26 Python