您發佈的代碼將不會做任何事情,只會選擇3個僞隨機數,直到時間結束。你必須添加一些贏/輸條件。截至目前,x,y和z只是數字。如果你想使一個賭博遊戲,你必須添加一些勝利條件,如:
if x + y + z > 10
只是一個例子,但你的程序需要能夠告訴如果玩家贏了。那麼它需要改變球員的總金額並要求新的投注。您還可能需要添加邏輯以確保玩家不能比他們下注更多。
import random
coins = 1000
wager = 0
while True: #main loop
print('you have {} coins'.format(coins))
if coins == 0: #stops the game if the player is out of money
print('You are out of money! Scram, deadbeat!')
break
while wager > coins or wager == 0: #loops until player enters a non-zero wager that is less then the total amount of coins
wager = int(input('Please enter your bet (enter -1 to exit): '))
if wager < 0: # exits the game if the player enters a negative
break
print('All bets are in!')
x = random.randint(0,10)
y = random.randint(0,10)
z = random.randint(0,10)
print(x,y,z) #displays all the random ints
if x + y +z > 10: #victory condition, adds coins for win
print('You win! You won {} coins.'.format(wager))
coins += wager
else: #loss and deduct coins
print('You lost! You lose {} coins'.format(wager))
coins -= wager
wager = 0 # sets wager back to 0 so our while loop for the wager validation will work
縮進在Python中很重要;你能否正確地在'while'後面縮進行以反映你的代碼? –