python實(shí)現(xiàn)用戶答題功能
python實(shí)戰(zhàn),用戶答題分享給大家。
主要包含內(nèi)容,文件的讀取,更改,保存。不同文件夾引入模塊。輸入,輸出操作。隨機(jī)獲取數(shù)據(jù)操作
隨機(jī)生成算數(shù)表達(dá)式,用戶輸入答案,正確記錄分?jǐn)?shù),錯(cuò)誤返回0,并把用戶分?jǐn)?shù)記錄到文本文件中,如用戶名不存在著新建用戶
myPythonFunction.py包含三個(gè)函數(shù)
#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 獲取隨機(jī)生成的數(shù)學(xué)表達(dá)式 ,返回字符串 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作為主函數(shù)
# -*- coding:utf-8 -*- import sys if 'H:\\python\func' not in sys.path: sys.path.append('H:\\python\\func') import myPythonFunction as myfunc print("請(qǐng)輸入你的名字:") use = input() use=use.strip("\n") count = 0 if(myfunc.getUserScore(use)==-1): print("你是個(gè)新用戶!") myfunc.updateUserPoints(use,0) else: count = int(myfunc.getUserScore(use)) print("你當(dāng)前分?jǐn)?shù)為:",count) while(1): questionString=myfunc.getQuestionString() result = eval(questionString) print("問題:",questionString.replace("**","^")) print("請(qǐng)輸入你的答案:") userResult = input() userResult = userResult.strip("\n") flag = True if(userResult.startswith("-")): userResult = userResult[1:] flag = False while((not userResult.isdigit()) and userResult!="exit"): print("請(qǐng)輸入數(shù)字,你的答案:") 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("你當(dāng)前分?jǐn)?shù)為:",count) print("謝謝進(jìn)入,歡迎下次再來!")
文件目錄結(jié)構(gòu),將myPythonFunction.py放到func文件夾中。userScores.txt存放用戶名和相應(yīng)的得分
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- python OpenCV實(shí)現(xiàn)答題卡識(shí)別判卷
- python實(shí)現(xiàn)百萬答題自動(dòng)百度搜索答案
- 答題輔助python代碼實(shí)現(xiàn)
- python3.5+tesseract+adb實(shí)現(xiàn)西瓜視頻或頭腦王者輔助答題
- 從0到1使用python開發(fā)一個(gè)半自動(dòng)答題小程序的實(shí)現(xiàn)
- Python沖頂大會(huì) 快來答題!
- Python答題卡識(shí)別并給出分?jǐn)?shù)的實(shí)現(xiàn)代碼
- python利用opencv如何實(shí)現(xiàn)答題卡自動(dòng)判卷
相關(guān)文章
Django連接數(shù)據(jù)庫并實(shí)現(xiàn)讀寫分離過程解析
這篇文章主要介紹了Django連接數(shù)據(jù)庫并實(shí)現(xiàn)讀寫分離過程解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-11-11python實(shí)現(xiàn)銀行管理系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了python實(shí)現(xiàn)銀行管理系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-10-10pandas計(jì)數(shù) value_counts()的使用
這篇文章主要介紹了pandas計(jì)數(shù) value_counts()的使用,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-06-06卷積神經(jīng)網(wǎng)絡(luò)經(jīng)典模型及其改進(jìn)點(diǎn)學(xué)習(xí)匯總
這篇文章主要為大家介紹了卷積神經(jīng)網(wǎng)絡(luò)經(jīng)典模型及其改進(jìn)點(diǎn)學(xué)習(xí)匯總,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-05-05Python調(diào)用.net動(dòng)態(tài)庫實(shí)現(xiàn)過程解析
這篇文章主要介紹了Python調(diào)用.net動(dòng)態(tài)庫實(shí)現(xiàn)過程解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-06-06