我将如何继续进行小型赌场游戏?

时间:2019-08-02 09:19:12

标签: python

我是编程新手,做了半场赌场游戏。要在此游戏中下注,您输入“ bet100”。首先下注,然后下注您要下注多少钱。我将如何赚取奖金,取决于您选择下注多少,从而增加/减少奖金?我可以不使用数十条if语句来做到吗?我该如何编程? 另外,我也做了这样的安排,使程序可以在没有单词“ bet”的情况下将赌注减少到自己的变量中。该变量称为数量。

import random
print("CasinoGame") #Miriam
money = 1000
gameActive = True
while gameActive:
    bet = input("""Write "bet" to bet 100$""")
    bet_len = len(bet)
    if bet_len == 0:
        print("bet is empty")
    bet_type = bet[:3]
    print(bet_type)
    amount = 0
    if bet_len > 3:
        num_string = bet[3:]
        amount = int(num_string)
    print(bet, bet_type, amount)

1 个答案:

答案 0 :(得分:0)

您的赌场游戏(这里的真实游戏是coinflip,但是您可以使用任何想要实现的游戏)看起来像这样:

import random

print("CasinoGame") 
money = 1000

while True:
    # 1) ask if player wants to continue to play
    continue_play = input('Write "bet" if you want to continue to play or "quit" if you want to leave the casino.\n')
    # if not: break the loop
    if continue_play == 'quit': break
    # if typo or something: ask again (next iteration)
    if continue_play != 'bet': continue
    # ask for bet size (has to be integer)
    bet = int(input(f'Enter the amount you want to bet (in numbers).\nYour money: {money}\n'))
    # check if user is able to make the bet, if not: tell how much money is left and start again (next iteration)
    if bet > money: 
        print(f'Sorry, but you cannot bet more money than you have. Your money: {money}.\n')
        continue
    # calculate possible winnings (bigger if the bet is bigger)
    # you could also do something like a exponential function
    poss_win = bet*3
    # flip a coin
    coin = random.randint(0,1)
    # see if the user won 
    if coin == 1: 
        win = True
    else:
        win = False
    # evaluate: give money if win, take if loose
    if win:
        print(f'Congratulations, you won {poss_win}.')
        money = money - bet + poss_win
    else:
        print(f'Sorry, but you lost {bet}.')
        money = money - bet
    # if the user is broke: throw him out of the casino 
    if money == 0:
        print('Sorry, you are broke. Thank you, come again.')
        break

# see & tell if user made or lost money (or is even)
if money < 1000:
    losses = 1000 - money
    print(f'You lost {losses} and have now {money}.')
elif money == 1000:
    print('You have neither lost nor won something.')
else:
    winnings = money - 1000
    print(f'Congratulations, you have won {winnings} and have now {money}.')

请注意,f字符串仅适用于Python版本> 3.6。同样,如果用户输入非整数作为下注,则脚本将中断。您可以实施tryexcept语句来避免这种情况。