游戲實作具體功能
原有的用戶登錄的資訊均能保存在txt檔案里,注冊的資訊也能保存在txt里面
格式如下:

便于之后轉換成串列字典形式添加注冊資料
用戶的猜拳記錄也能保存在txt中并以如下格式顯示
格式如下:
具體Python代碼:
#-*- coding=utf8 -*-
# @author:sololi
# date: 2020/10/24
# 檔案說明 :
# 123檔案存放 用戶資訊 456 檔案存放猜拳結果
import sys
fff= open('123',"r+",encoding="utf8")
shuju=[]
a=[]
b=[]
c=[]
aa={}
username=0
for line in fff.readlines():
line=line.strip('\n')
a= line.split(' ')
i=0
while i<len(a):
b=a[i].split(',')
i+=1
j=0
while j<len(b):
if b==" ":
break
c=b[j].split(':',1)
aa[c[0]]=c[1]
i+=1
j+=1
shuju.append(aa.copy()) #將資料 轉換成串列字典形式便于后面登錄與注冊存放資料(copy是為了防止添加是資料型別不同出錯)
fff.close()
print("#######################")
print("歡迎進入猜拳游戲")
print("#######################")
denglu=input("登錄輸入1,注冊輸入2") #登錄程式
if denglu=="1" :
while True:
username = input("請輸入用戶名")
jg1= 0
i = 0
while(i<len(shuju)):
if (username == shuju[i]["用戶名"]):
print("用戶名正確")
jg1=1
break
i+=1
if(jg1!=1):
print("用戶名錯誤")
elif (jg1 == 1):
break
while True:
password = input("請輸入密碼")
jg2=0
i = 0
while(i<len(shuju)):
if (password == shuju[i]["密碼"]):
print("密碼正確")
jg2=1
break
i+=1
if(jg2!=1):
print("密碼錯誤")
elif(jg2==1):
break
elif denglu=="2" :
while True:
username = input("請輸入注冊的用戶名")
jg3 = 0
i = 0
while (i < len(shuju)):
if (username == shuju[i]["用戶名"]):
print("用戶名已經存在")
jg3 = 1
break
i += 1
if(jg3 == 0):
password = input("請輸入注冊的密碼")
print("注冊成功")
print("登錄成功")
fff = open("123", mode='a+', encoding="utf8")
fff.seek(2)
fff.write("用戶名:{},密碼:{}\n".format(username, password))
fff.close()
break
else:
print("輸入有誤,結束登錄")
sys.exit(0)
while True: #猜拳游戲程式
xuanzhe = (input("進行游戲1\n顯示資訊2\n退出游戲3"))
if xuanzhe=="1":
z=0.0
c=0.0
import random
while True:
guess=random.randint(1,3)
num=int(input("選擇石頭(1)/剪刀(2)/布(3)"))
if (num==1 and guess==2) or (num==2 and guess==3) or (num==3 and guess==1):
print("勝")
c=c+1
elif num ==guess:
print("平")
elif(num==1 and guess==3) or (num==2 and guess==1) or (num==3 and guess==2):
print("負")
else:
print("輸入有誤")
continue
jieguo=input("是否繼續游戲,輸入任意退出游戲")
z = z + 1
if jieguo=="是":
continue
else: break
kd=((c/z))
print('總數{:.0f}' '勝率:{:.2%}'.format(z,kd))
f=open("456",mode='a+',encoding="utf8")
f.write("用戶名:{},總數:{:.0f},勝率:{:.2%}\n".format(username,z, kd))
f.close()
elif xuanzhe=="2":
f = open("456","r+",encoding="utf8")
res = f.read()
print(res)
f.close()
elif xuanzhe=="3":
break
else:
print("輸入有誤請重新輸入")
print("歡迎再次使用猜拳游戲")
初學者作品,有錯大神請指正
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/197679.html
標籤:其他
