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 相关文章推荐
对pandas中Series的map函数详解
Jul 25 Python
Python计算库numpy进行方差/标准方差/样本标准方差/协方差的计算
Dec 28 Python
python实现转盘效果 python实现轮盘抽奖游戏
Jan 22 Python
浅谈python标准库--functools.partial
Mar 13 Python
python接口自动化(十七)--Json 数据处理---一次爬坑记(详解)
Apr 18 Python
Python解析json时提示“string indices must be integers”问题解决方法
Jul 31 Python
python matplotlib库绘制散点图例题解析
Aug 10 Python
python3 selenium自动化 下拉框定位的例子
Aug 23 Python
Python3使用xlrd、xlwt处理Excel方法数据
Feb 28 Python
Django多层嵌套ManyToMany字段ORM操作详解
May 19 Python
python使用多线程+socket实现端口扫描
May 28 Python
pytorch 查看cuda 版本方式
Jun 23 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防注
2007/01/15 PHP
PHP学习笔记之一
2011/01/17 PHP
php 目录遍历、删除 函数的使用介绍
2013/04/28 PHP
PHP框架Laravel学习心得体会
2015/10/28 PHP
php实现异步数据调用的方法
2015/12/24 PHP
通过js脚本复制网页上的一个表格的不错实现方法
2006/12/29 Javascript
我遇到的参数传递中 双引号单引号嵌套问题
2010/02/11 Javascript
分享33个jQuery与CSS3实现的绚丽鼠标悬停效果
2014/12/15 Javascript
jquery控制页面的展开和隐藏实现方法(推荐)
2016/10/15 Javascript
微信小程序 WXDropDownMenu组件详解及实例代码
2016/10/24 Javascript
bootstrap PrintThis打印插件使用详解
2017/02/20 Javascript
详解nodejs通过响应回写的方式渲染页面资源
2018/04/07 NodeJs
深入解析vue 源码目录及构建过程分析
2019/04/24 Javascript
Vuex新手的理解与使用详解
2019/05/31 Javascript
微信小程序仿淘宝热搜词在搜索框中轮播功能
2020/01/21 Javascript
ant design pro中可控的筛选和排序实例
2020/11/17 Javascript
python每隔N秒运行指定函数的方法
2015/03/16 Python
Java Web开发过程中登陆模块的验证码的实现方式总结
2016/05/25 Python
Python编程实现tail-n查看日志文件的方法
2019/07/08 Python
python 搜索大文件的实例代码
2019/07/08 Python
python正则爬取某段子网站前20页段子(request库)过程解析
2019/08/10 Python
使用python获取邮箱邮件的设置方法
2019/09/20 Python
python批量修改xml属性的实现方式
2020/03/05 Python
Django模型中字段属性choice使用说明
2020/03/30 Python
Django自定义YamlField实现过程解析
2020/11/11 Python
AmazeUI 手机版页面的顶部导航条Header与侧边导航栏offCanvas的示例代码
2020/08/19 HTML / CSS
世界上最好的足球商店:Unisport
2019/03/02 全球购物
Pandora德国官网:购买潘多拉手链、戒指、项链和耳环
2020/02/20 全球购物
学生拾金不昧表扬信
2014/01/21 职场文书
简历的自我评价范文
2014/02/04 职场文书
2014年小学校长工作总结
2014/12/08 职场文书
明星邀请函
2015/02/02 职场文书
美术教师求职信范文
2015/03/20 职场文书
高一化学教学反思
2016/02/22 职场文书
nginx配置ssl实现https的方法示例
2021/03/31 Servers
python如何为list实现find方法
2022/05/30 Python