python代码实现猜拳小游戏
作者:江liuer_666 时间:2023-10-16 19:00:46
本文实例为大家分享了python代码实现猜拳小游戏的具体代码,供大家参考,具体内容如下
游戏实现具体功能
原有的用户登录的信息均能保存在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://blog.csdn.net/weixin_51322568/article/details/109367313
标签:python,猜拳
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
深入探究Go语言从反射到元编程的实践与探讨
2024-05-22 10:28:50
Python中使用ConfigParser解析ini配置文件实例
2021-06-18 01:41:44
详解python数值与字符串高级用法
2021-08-07 15:05:52
简述php环境搭建与配置
2023-11-15 09:08:28
使用django实现一个代码发布系统
2022-06-28 02:42:45
![](https://img.aspxhome.com/file/2023/4/65114_0s.png)
golang基于websocket实现的简易聊天室程序
2023-06-15 00:27:01
js实现文字列表无缝滚动效果
2024-04-19 10:16:12
Linux中大内存页Oracle数据库优化的方法
2023-07-19 11:24:57
![](https://img.aspxhome.com/file/2023/6/64436_0s.jpg)
PyQt5 designer 页面点击按钮跳转页面实现
2023-02-24 14:12:48
![](https://img.aspxhome.com/file/2023/0/109860_0s.png)
你不知道的Vue技巧之--开发一个可以通过方法调用的组件(推荐)
2024-04-30 08:46:07
CSS.JS文件发布机制的思考
2009-08-04 13:07:00
Python变量格式化输出实现原理解析
2023-02-12 23:13:51
对Python生成汉字字库文字,以及转换为文字图片的实例详解
2021-12-10 16:55:51
![](https://img.aspxhome.com/file/2023/8/102868_0s.jpg)
mysql如何查询日期与时间
2024-01-26 02:03:17
![](https://img.aspxhome.com/file/2023/8/112868_0s.png)
利用Axure封装视觉标准
2008-10-21 10:42:00
![](https://img.aspxhome.com/file/UploadPic/200810/21/20081021104738239s.jpg)
利用J2ME与ASP建立数据库连接
2010-04-03 20:53:00
在 Golang 中实现一个简单的Http中间件过程详解
2024-05-09 14:55:35
![](https://img.aspxhome.com/file/2023/6/127376_0s.png)
MySQL 外键约束和表关系相关总结
2024-01-17 00:09:26
String.indexOf 方法介绍
2013-06-01 20:22:27
python不相等的两个字符串的 if 条件判断为True详解
2021-12-12 16:54:51
![](https://img.aspxhome.com/file/2023/8/128638_0s.jpg)