国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

python實現用戶答題功能

2020-02-22 22:53:02
字體:
來源:轉載
供稿:網友

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("謝謝進入,歡迎下次再來!")             
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 柞水县| 永兴县| 南溪县| 镇远县| 宜州市| 安龙县| 潮州市| 白山市| 始兴县| 类乌齐县| 佛冈县| 获嘉县| 施秉县| 古浪县| 温宿县| 玛曲县| 岳阳市| 资溪县| 淮南市| 桦甸市| 彰化市| 宜城市| 交城县| 雅江县| 凌源市| 云阳县| 巴彦淖尔市| 会泽县| 普格县| 荣昌县| 宜宾县| 桂平市| 喀喇| 裕民县| 敦煌市| 广东省| 平陆县| 星子县| 临夏县| 吉林省| 潜山县|