python搖骰子游戲小案例

2020-10-16 14:00:30

python搖骰子游戲小案例

小案例:
import random

#充值函數
def topup():
    global money
    n = input('充值金額(100元等於30個金幣充值金額不低於100元):')
    num = int(n)
    if(num % 100 == 0 and num > 0):
        money = (num//100)*30
        print("*"*10+"充值成功"+'*'*10)
        print('當前賬戶:{}---您當前金幣剩餘:{}枚'.format(username, money))

#搖色子函數
def debit():
    global money
    first = random.randint(1, 6)
    last = random.randint(1, 6)
    money -= 2
    print('系統已經洗牌完畢!!!')
    guress = input('請您輸入大小(大/小)')
    if(((first+last > 6) and guress == '大') or ((first+last <= 6) and guress == '小')):
        print('恭喜您本局遊戲勝利!本局遊戲獎勵4和遊戲金幣')
        money += 6
        print('當前賬戶:{}---您當前金幣剩餘:{}枚'.format(username, money))
        answer = input('是否繼續遊戲嗎?繼續遊戲需要扣除2枚金幣(y/n)')
        debit()
    else:
        print('很遺憾本局遊戲失敗@-@')
        print('當前賬戶:{}---您當前金幣剩餘:{}枚'.format(username, money))
        answer = input('是否繼續遊戲嗎?繼續遊戲需要扣除2枚金幣(y/n)')
        if(answer != 'y' or money < 2):
            print('........退出遊戲...........')
        else:
            debit()


username = input('請輸入您的賬號:')
money = 4
answer = input('您確定進入遊戲嗎(y/n)')


if (answer == 'y'):
    if money < 2:
        answer = input(
            "當前賬戶:{}---您當前金幣剩餘:{}枚是否充值(y/n)".format(username, money))
        if(answer == 'y'):
            topup()
            debit()
    else:
        if(answer == 'y'):
            print('歡迎{}進入遊戲'.format(username))
            while True:
                debit()

注:學了一點點python寫的小案例