python 掷骰子

import random

CHOICES = ['big', 'small']
INIT_MONEY = 1000


def bet(int_money):
    """进行每局游戏"""
    str_choice = get_str_choice()
    int_bet_money = get_int_bet_money(int_money)
    # 返回三个骰子的点数
    list_roll_point = roll_dice()
    str_roll_result = get_str_roll_result(list_roll_point)

    if str_choice == str_roll_result:
        print("The points are {}, You Win!".format(list_roll_point))
        int_money += int_bet_money
        print("You gained ${}, you have ${} now.\n".format(int_bet_money, int_money))
    else:
        print("The points are {}, You Lose!".format(list_roll_point))
        int_money -= int_bet_money
        print("You lost ${}, you have ${} now.\n".format(int_bet_money, int_money))

    return int_money


def get_str_choice():
    """获取玩家选择"""
    while True:
        str_choice = input("Big or Small: ").lower()
        if str_choice not in CHOICES:
            print("Invalid Choice")
        else:
            return str_choice


def get_int_bet_money(int_money):
    """获取玩家赌金"""
    while True:
        try:
            int_bet_money = int(input("How much you wanna bet? "))
        except ValueError:
            print("sorry,只能是整数")
            continue

        if int_bet_money == 0:
            print("没钱别玩!")
        elif int_bet_money > int_money:
            print("No more money!")
        else:
            return int_bet_money


def roll_dice():
    """掷骰子,返回骰子点数"""
    print("<<<<< ROLL THE DICE! >>>>>")
    list_roll_point = []
    for dice in range(3):
        list_roll_point.append(random.randint(1, 6))
    return list_roll_point


def get_str_roll_result(list_roll_point):
    """获取骰子结果,大或者小"""
    if 11 <= sum(list_roll_point) <= 18:
        return CHOICES[0]
    else:
        return CHOICES[1]


def main():
    """控制游戏开始,结束,继续"""
    print("\n\n" + "<" * 30 + " GAME STARTS! " + ">" * 30)
    print("You have ${} now.\n".format(INIT_MONEY))
    int_money = bet(INIT_MONEY)

    while int_money > 0:
        int_money = bet(int_money)
    else:
        print("GAME OVER!")
        if input("press r to restart game:\n") == 'r':
            main()


main()

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值