我是编程的新手,并且制作了赌场游戏。要在此游戏中押注您写&quot'bet100&quot。您写下注 要投注多少钱。我将如何制作,以使奖金变得更大/较小,具体取决于您决定押注多少?我该如何编码?
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)
您的赌场游戏(这里的真实游戏是共同的flip,但您可以使用要实现的任何东西(可以看起来像这样:
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-string仅在Python版本> 3.6中起作用。另外,如果用户输入除整数以外的其他内容,则该脚本将中断。您可以实现try
和except
语句以避免。