猜拳游戏

需求分析:
* 使用面向对象和python的基础语法,运用简单的逻辑处理实现猜拳游戏
* 要求游戏可以多次玩耍
* 要求统计分数
* 要求可以选择角色

# 玩家自己的类,
class Owns():chose = {1: "石头", 2: "剪刀", 3: "布"}def __init__(self, name, race):self.name = nameself.race = raceself.score = 0def playgame(self):while True:print("游戏规则:")for index,i in self.__class__.chose.items():print(f"{index}:{i}",end="\t")print()chose = input("请输入您的选择>>>")if chose.isdigit() and 1 <= int(chose) <= 3:chose = int(chose)print(f"您选择了{self.__class__.chose[chose]}")return choseelse:print("输入有误")def __str__(self):return f"我方>>>>>>姓名:{self.name}\t种族:{self.race}"
# 定义电脑玩家的类
class Computer(Owns):super(Owns)def __str__(self):return f"敌方>>>>>>姓名:{self.name}\t种族:{self.race}"
# 游戏逻辑处理
class Game():race = {1: ("人族", "仁爱而睿智"), 2: ("羊族", "温顺而纯洁"), 3: ("天族", "高贵而强悍")}def myrole_generator(self, name, race):return Owns(name, race)def computer_generator(self, name, race):return Computer(name, race)def chose_role(self):while True:for index, i in self.__class__.race.items():print(f"{index}:{i[0]}~~~{i[1]}")chose = input("请选择您的种族>>>")if chose.isdigit() and 1 <= int(chose) <= 3:name = input("请设计您的名字>>>")my1 = self.myrole_generator(name, self.__class__.race[int(chose)][0])chose = input("请选择对战电脑的种族>>>")if chose.isdigit() and 1 <= int(chose) <= 3:name = input("请设计电脑的名字>>>")com1 = self.myrole_generator(name, self.__class__.race[int(chose)][0])else:print("输入错误,请重新输入")continueelse:print("输入错误,请重新输入")continuereturn my1, com1def play(self):while True:print("欢迎来到猜拳游戏,祝您玩的开心(*^▽^*)")lisrole = self.chose_role()my1 = lisrole[0]com1 = lisrole[1]print("对战信息>>")print(my1, com1, sep="\n")print("<<<<<<<游戏开始>>>>>>>")import randomflag = Truewhile flag:mychose = my1.playgame()comchose = random.randint(1, 3)print(f"电脑选择了{com1.__class__.chose[comchose]}")if mychose == comchose:print("平局")elif mychose == 1 and comchose == 2 or mychose == 2 and comchose == 3 or mychose == 3 and comchose == 1:my1.score += 1print("您赢了")else:com1.score+=1print("您输了,2233333333333")while True:chose = input("是否继续愉快的玩耍Y(继续)/N(退出)/S(重新选择角色),????")if chose.isalpha() and chose.upper() == "N":print(f"对战信息:\n{my1.name}:{my1.score}\n{com1.name}:{com1.score}")print("猜拳游戏结束,欢迎下次来happy~~~~~")exit()elif chose.isalpha() and chose.upper() == "Y":breakelif chose.isalpha() and chose.upper() == "S":print(f"当前对战信息:\n{my1.name}:{my1.score}\n{com1.name}:{com1.score}")flag = Falseelse:continueif flag == False:break
# 主函数,调用端口a1 = Game()a1.play()

扎金花游戏

需求:
编写程序,设计单张扑克牌类Card,具有花色,牌面与具体值。
同时设计整副扑克牌类Cards,具有52张牌。li=[1,2,3] li=[card1,card2,card3…]
红桃、黑桃、方片、草花 2345678910JQKA
♥♠♦♣
设计一个发牌的函数,可以任意发出三张牌。
对任意三张牌断定牌的类型。
类型包括:
三条:三张牌value一样
一对:两张value一样
顺子:三张牌挨着
同花:三张牌type一样
同花顺:挨着,类型一样
其余都是散牌

# 单张扑克类
class card():def __init__(self,type,value,tvalue):self.type = typeself.value = valueself.tvalue = tvaluedef __str__(self):return f"[{self.type}{self.value}]"
# 扑克牌的类
class cards():def __init__(self):types = "♥♠♦♣"values = [i for i in range(2,11)] +list("JQKA")self.lis = []for i in types:for index,j in enumerate(values):self.lis.append(card(i,j,index+2))self.li = []def deal(self):self.li = []import randomfor i in range(3):index = random.randint(0, len(self.lis) - 1)self.li.append(self.lis.pop(index))return self.li
# 玩家类  和   电脑玩家类
class Owns():def __init__(self, name, race):self.name = nameself.race = racedef __str__(self):return f"我方>>>>>>姓名:{self.name}\t种族:{self.race}"def playgame(self,li):print("您的牌如下:")for index,i in enumerate(li):print(f"{index+1}:{i}")chose = input("您可以选择Y(比牌)/N(放弃)>>>")if chose.isalpha() and chose.upper() == "Y":return Trueelse:return False
class Computer(Owns):super(Owns)
# 逻辑处理类,游戏规则制定
class Game():race = {1: ("人族", "仁爱而睿智"), 2: ("羊族", "温顺而纯洁"), 3: ("天族", "高贵而强悍")}def myrole_generator(self, name, race):return Owns(name, race)def computer_generator(self, name, race):return Computer(name, race)def chose_role(self):while True:for index, i in self.__class__.race.items():print(f"{index}:{i[0]}~~~{i[1]}")chose = input("请选择您的种族>>>")if chose.isdigit() and 1 <= int(chose) <= 3:name = input("请设计您的名字>>>")my1 = self.myrole_generator(name, self.__class__.race[int(chose)][0])chose = input("请选择对战电脑的种族>>>")if chose.isdigit() and 1 <= int(chose) <= 3:name = input("请设计电脑的名字>>>")com1 = self.myrole_generator(name, self.__class__.race[int(chose)][0])else:print("输入错误,请重新输入")continueelse:print("输入错误,请重新输入")continuereturn my1, com1def getResult(self,li):li.sort(key=lambda k: k.tvalue)one = li[0]  # type,value,real_valuesecond = li[1]third = li[2]if one.value == second.value == third.value:return  "三条"elif one.value == second.value or second.value == third.value:return "一对"elif (one.type == second.type == third.type) and (one.tvalue + 1 == second.tvalue and second.tvalue + 1 == third.tvalue):return "同花顺"elif one.tvalue + 1 == second.tvalue and second.tvalue + 1 == third.tvalue:return "顺子"elif one.type == second.type == third.type:return "同花"else:return "散牌"def play(self):while True:print("欢迎来到炸金花炸死你游戏,祝您玩的开心(*^▽^*)")lisrole = self.chose_role()my1 = lisrole[0]com1 = lisrole[1]print("对战信息>>")print(my1, com1, sep="\n")print("<<<<<<<游戏开始>>>>>>>")while True:cards1 = cards()li = cards1.deal()flag = my1.playgame(li)if flag == True:li1 = cards1.deal()dresult = self.getResult(li1)myresult = self.getResult(li)if dresult == myresult:if li > li1:print(f"your is {myresult},he/she is {dresult}")print("对方的牌:")for index, i in enumerate(li1):print(f"{index+1}:{i}")print("您的牌:")for index, i in enumerate(li):print(f"{index+1}:{i}")print("恭喜你,你赢了,666")else:print(f"your is {myresult},he/she is {dresult}")print("对方的牌:")for index, i in enumerate(li1):print(f"{index+1}:{i}")print("您的牌:")for index, i in enumerate(li):print(f"{index+1}:{i}")print("哈哈哈哈,你输了,223333333")else:if myresult == "三条":print(f"your is {myresult},he/she is {dresult}")print("恭喜你,你赢了,666")elif myresult == "同花顺" and dresult != "三条":print(f"your is {myresult},he/she is {dresult}")print("恭喜你,你赢了,666")elif myresult == "同花" and dresult not in ["三条", "同花顺"]:print(f"your is {myresult},he/she is {dresult}")print("恭喜你,你赢了,666")elif myresult == "顺子" and dresult not in ["三条", "同花顺", "同花"]:print(f"your is {myresult},he/she is {dresult}")print("恭喜你,你赢了,666")elif myresult == "一对" and dresult not in ["三条", "同花顺", "同花", "顺子"]:print(f"your is {myresult},he/she is {dresult}")print("恭喜你,你赢了,666")elif myresult == "散牌":print(f"your is {myresult},he/she is {dresult}")print("哈哈哈哈,你输了,223333333")chose = input("您要继续愉快的玩耍么?>>>Y/N/exit")if chose.upper() == "Y":continueelif chose.upper() == "N":print("重新开局中~~~~~")breakelif chose.upper() == "EXIT":print("游戏结束,欢迎下次来受虐")exit()else:print("重新开局中~~~~~")break
# 游戏入口
g1 = Game()
g1.play()

小型购物系统

需求:
要求用户输入总资产,例如:2000
显示商品列表,让用户根据序号选择商品,加入购物车
购买,如果商品总额大于总资产,提示账户余额不足,否则,购买成功。
附加:可充值、某商品移除购物车

shopping_list = [("苹果手机", 5000),("破手机壳", 48),("电纸书facebook", 9800),("华为手机", 4800),("python书籍", 32),("垃圾一堆", 24)
]
#购物车
shopping_cart = []
salary = input('请输入您的总资产: ')
#如果不是数字
if not salary.isdigit():print("总资产必须是数字,请重新输入")#中断程序exit()
else:#将资产转化为十进制salary = int(salary)
while True:print("-----------产品列表-----------")for index, item in enumerate(shopping_list):#print("\033[32m%s, %s\033[0m" %(index, item))print(f"\033[32m{index},\033[10m{item}")choice = input('请选择您要购买的东西>>>')if choice.isdigit():choice = int(choice)if choice < len(shopping_list) and choice >= 0:product = shopping_list[choice]if salary > product[1]:confirm = input('您想现在购买这个商品么?[y/n]: ')if confirm == 'y':shopping_cart.append(product)salary -= product[1]print(f"您买了 {product[0]},价格是 {product[1]}, 您剩余 {salary}")else:print('请再次选择')else:add_confirm = input(f"您的余额为: {salary}, 不够, 您想充钱么?[y/n]")if add_confirm == 'y':add_salary = input('请输入您要充的钱: ')if add_salary.isdigit():add_salary = int(add_salary)salary += add_salaryprint(f"现在您的余额是{salary}: ")else:print("钱数必须是数字")else:print("------您的购物车---------")for index, item in enumerate(shopping_cart):print(index, item)else:print("您必须选择0~5以内的商品")elif choice == 'q':remove_product = input("您现在要删除产品还是退出 [y/n] ")if remove_product == "y":print("-----------购物车列表-------------: ")for index, item in enumerate(shopping_cart):print(index, item)remove_choice = input('请选择您要删除的商品>>> ')if remove_choice.isdigit() and int(remove_choice) < len(shopping_cart) and int(remove_choice) >= 0:salary += shopping_cart[int(remove_choice)][1]del shopping_cart[int(remove_choice)]print("-----------新的购物车列表-------------: ")for index, item in enumerate(shopping_cart):print(index, item)print(f"your balance is {salary}")else:print("input error, again")else:print("exit now")exit()else:print("-----------购物车列表-------------: ")for index, item in enumerate(shopping_cart):print(index, item)print("\033[31m选择必须是数字,退出\033[0m")

python基础练习(猜拳游戏、扎金花游戏、购物小程序)相关推荐

  1. 扎金花游戏 PHP 实现代码之大小比赛

    扎金花游戏 PHP 实现代码之大小比赛 程序离不开算法,在前面的博客当中,其实我们已经讨论过寻路的算法.不过,当时的示例图中,可选的路径是唯一的.我们挑选一个算法,就是说要把这个唯一的路径选出来,怎么 ...

  2. 扎金花 游戏开发细节与部分代码

    扎金花 游戏开发细节与部分代码,斗地主游戏中的牌型很多,算法也各有不同,但我总觉得网上一些通用的算法有点铺天盖地,所以,我决定自己来重新想一下算法,总的来说,无论你出什么处于,三带二,炸,4带一等,你 ...

  3. PHP服务端、Unity客户端 双端基础源码做avalon阿瓦隆桌游面sha(类似狼人游戏)支持WebGL、小程序发布

    文章目录 PHP服务端发布(Windows下演示) Windows 安装PHP 启动服务器 Linux家族 Unity客户端发布 发布Windows客户端 发布WebGL端 演示 源码解析 联系作者 ...

  4. 猜歌小游戏多功能组合微信小程序源码下载

    这是一款多功能游戏组合的一款小程序 比如猜歌,摇骰子,真心话大冒险等等 php7.2 mysql5.6 1.上微擎框架 2.将后台两个压缩包上传到addons目录下解压 创建小程序应用 3.后台设置一 ...

  5. 2019小程序赚钱全攻略:零基础搭建、引爆、变现你的小程序

    最近在知乎有个问题爆红: 抖出无数人的心酸经历: 无论买什么东西,都会不由自主跟每天的饭钱作对比-- 父亲做手术,我却负担不起昂贵的止疼药-- 老公出轨,却因为没钱不敢离婚,怕抢不到孩子的抚养权-- ...

  6. python写一个表白程序_用Python写一个能算出自己年龄的小程序

    用Python写一个能算出自己年龄的小程序. 其实我连我今年多少岁都不知道,最近看到了python的datetime库里面有很多好用的方法,于是就写了这样一个程序作为练习,然后又写了这样一篇文章来梳理 ...

  7. 如何用python计算年龄_用Python写一个能算出自己年龄的小程序

    用Python写一个能算出自己年龄的小程序. 其实我连我今年多少岁都不知道,最近看到了python的datetime库里面有很多好用的方法,于是就写了这样一个程序作为练习,然后又写了这样一篇文章来梳理 ...

  8. [附源码]计算机毕业设计Python+uniapp基于微信支付的在线打印微信小程序ah1u9(程序+lw+远程部署)

    [附源码]计算机毕业设计Python+uniapp基于微信支付的在线打印微信小程序ah1u9(程序+lw+远程部署) 该项目含有源码.文档.程序.数据库.配套开发软件.软件安装教程 项目运行环境配置: ...

  9. python聊天小程序支持私聊和多人_利用Python打造一个多人在线匿名聊天的小程序!(前后端完整开发)...

    用Python打造一个多人在线匿名聊天的小程序(附代码) 最近看到好多设计类网站, 都提供了多人在线匿名聊天的小功能, 感觉很有意思, 于是自己就用django框架写了一个, 支持手动实时更名, py ...

最新文章

  1. css字体居中_简单介绍CSS.
  2. hdu 5511 Minimum Cut-Cut——分类讨论思想+线段树合并
  3. 信息系统项目管理师:第4章:项目整体管理与变更管理(1)
  4. python抖音github_GitHub - eternal-flame-AD/Douyin-Bot: Python 抖音机器人,论如何在抖音上找到漂亮小姐姐?...
  5. Hybris服务器启动日志分析
  6. java显示星期几_Java 使用日历显示星期几
  7. 国内2大Git代码托管网站
  8. 列注释_机器学习 Pandas 03:基础 前16题 ( 带答案、注释 )
  9. 计算机特殊符号大全集,{精心收藏}电脑输入特殊字符大全
  10. 交换机的 VTP sever 与 client设置
  11. pycharm hotkey
  12. android 停止服务执行,android - 为什么在停止服务(执行onDestroy已执行)后,服务中的变量没有“重置”?...
  13. jQuery jqGrid 文档
  14. 万圣节字体来啦!6款风格奇幻的中文字体免费下载
  15. 开题报告的前景_开题报告全分析,写出一份满意的答卷
  16. “贵人”相助,亚马逊云科技APN成员乘风破浪
  17. 个人搭建独立博客,哪个程序比较好用
  18. 圣诞邀请助力活动H5系统开发
  19. 金多多配资提示指数方面不用过火纠结
  20. php 九宫格验证码,PHP+Ajax微信手机端九宫格抽奖实例

热门文章

  1. 电影-非常人贩(3)
  2. 歌曲 Feuille d’automne 歌词及释义
  3. 机器学习强基计划0-4:通俗理解奥卡姆剃刀与没有免费午餐定理
  4. 房地产ar大屏互动展示更炫酷吸睛
  5. 一个让人灵光一闪的数组C++类Array设计,可以此作为范本进行其他的C++类编写
  6. python例题求乘客等车时间_利用Python数据处理进行公交车到站时间预测(一)
  7. python row_row python
  8. 10.设计模式之桥接模式
  9. 离散余弦变换(Discrete Cosine Transform)
  10. ShaderForge-模型拉扯效果