python實現(xiàn)用戶答題功能
python實戰(zhàn),用戶答題分享給大家。
主要包含內(nèi)容,文件的讀取,更改,保存。不同文件夾引入模塊。輸入,輸出操作。隨機(jī)獲取數(shù)據(jù)操作
隨機(jī)生成算數(shù)表達(dá)式,用戶輸入答案,正確記錄分?jǐn)?shù),錯誤返回0,并把用戶分?jǐn)?shù)記錄到文本文件中,如用戶名不存在著新建用戶
myPythonFunction.py包含三個函數(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("請輸入你的名字:")
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("你當(dāng)前分?jǐn)?shù)為:",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("請輸入數(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)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
關(guān)于python中不同函數(shù)讀取圖片格式的區(qū)別淺析
這篇文章主要給大家介紹了關(guān)于python中不同函數(shù)讀取圖片格式的區(qū)別,文中通過實例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2022-03-03
解決tf.keras.models.load_model加載模型報錯問題
這篇文章主要介紹了解決tf.keras.models.load_model加載模型報錯問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-06-06
python3 tkinter實現(xiàn)添加圖片和文本
這篇文章主要為大家詳細(xì)介紹了python3 tkinter實現(xiàn)添加圖片和文本,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2019-11-11
Jupyter Notebook如何導(dǎo)入python文件時的問題
這篇文章主要介紹了Jupyter Notebook如何導(dǎo)入python文件時的問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-07-07

