zoukankan      html  css  js  c++  java
  • 用python设计猜大小的游戏

    import random
    
    def roll_dice(numbers = 3,points = None):
        print("------摇骰子------")
        if points is None:
            points = []
        while numbers > 0:
            point  = random.randrange(1,7)
            points.append(point)
            numbers = numbers - 1
        return points
    
    def roll_result(total):
        isbig = 11 <= total <= 18
        issmall = 3 <= total <= 10
        if isbig:
            return "大"
        elif issmall:
            return "小"
    
    def start_game():
        your_money = 1000
        while your_money > 0:
            print("-----游戏开始-----")
            choices = ["大","小"]
            your_choice = input("请下注,大 or 小:")
            your_bet = input('下注金额:')
            if your_choice in choices:
                points = roll_dice()
                total = sum(points)
                youwin = your_choice == roll_result(total)
                if youwin:
                    print("骰子点数:",points)
                    print("恭喜,你赢了{}元,你现在有{}元本金".format(your_bet,your_money + int(your_bet)))
                    your_money = your_money + int(your_bet)
                else:
                    print("骰子点数:",points)
                    print("很遗憾,你输了{}元,你现在有{}元本金".format(your_bet,your_money - int(your_bet)))
                    your_money = your_money - int(your_bet)
            else:
                print("格式有误,请重新输入")
        else:
            print("-----游戏结束-----")
    
    start_game()

    执行结果:

    -----游戏开始-----
    请下注,大 or 小:额
    下注金额:500
    格式有误,请重新输入
    -----游戏开始-----
    请下注,大 or 小:大
    下注金额:500
    ------摇骰子------
    骰子点数: [6, 5, 2]
    恭喜,你赢了500元,你现在有1500元本金
    -----游戏开始-----
    请下注,大 or 小:大
    下注金额:1500
    ------摇骰子------
    骰子点数: [6, 2, 3]
    恭喜,你赢了1500元,你现在有3000元本金
    -----游戏开始-----
    请下注,大 or 小:大
    下注金额:3000
    ------摇骰子------
    骰子点数: [4, 4, 4]
    恭喜,你赢了3000元,你现在有6000元本金
    -----游戏开始-----
    请下注,大 or 小:大
    下注金额:6000
    ------摇骰子------
    骰子点数: [5, 1, 1]
    很遗憾,你输了6000元,你现在有0元本金
    -----游戏结束-----
    

    本文转载自http://www.cnblogs.com/duwangdan/p/6835950.html,感谢作者。

  • 相关阅读:
    OPC客户端的进程安全初始化
    [精华] Oracle安装(linux)总结一下[转]
    Linux防火墙iptables的设置与启动[转]
    Linux Server 5.5安装SVN+Apache服务[转]
    Red hat Linux Enterprise 5.4 Edtion 学习笔记[二]
    RedHat Linux 5企业版开启VNCSERVER远程桌面功能[转]
    Linux服务配置:Vsftp的基本配置[转]
    Linux查看和剔除当前登录用户
    Ubuntu10.04的中文问题汇集与解决[转]
    Linux下扩展swap分区的方法
  • 原文地址:https://www.cnblogs.com/fanren224/p/8457250.html
Copyright © 2011-2022 走看看