[英]Python gambling dice game?
我是學習 Python 的新手,並且正在努力創建這個擲骰子賭博游戲。 我已經在它上面度過了愉快的一周,但仍然對無法使其正常/完全工作感到沮喪。 基本上我很難讓它正確打印並繼續詢問用戶的擲骰子猜測(2到12之間)。 我提前感謝任何人的幫助和建議!
您的total_bank()
方法可能會導致問題。
def total_bank():
bank = 500
while bank > 0:
print("You have ${bank} in your bank.")
bet = int(input("Enter your bet: "))
當你調用這個函數時,它會告訴你你的銀行有多少錢,一旦你輸入你的賭注,它會再次這樣做,因為銀行仍然 >0。 所以,你只會被困在這個無限循環中。
此外,在這種情況下,您將需要大量global
關鍵字。 雖然不建議使用它,但我認為在這種情況下很好。 您實際上並未更改銀行金額 - 該bank
變量是一個局部變量,這意味着您無法在total_bank
函數之外訪問它。 每次給bank
打電話都得還錢。
所以,代碼應該是
def total_bank():
bank = 500
print(f"You have ${bank} in your bank.")
# you forgot to make in a f-string
bet = int(input("Enter your bet: "))
bank-=bet
return bank
但是,它可能不適合您的目的。 例如,您可以查看限制賭注等。我只是在簡化它。
像這樣的東西可能是你真正想要的,但我不確定。
def total_bank():
bank = 500
print(f"You have ${bank} in your bank.")
# you forgot to make in a f-string
while bet <= 500: #whatever number is fine
bet = int(input("Enter your bet: "))
return bank, bet
bank, bet = total_bank()
希望這能回答你的問題!
'F' 字符串
F 字符串將花括號 {} 中的任何內容替換為表達式的值,例如函數的返回值或任何變量/值:
print(f"abc{1+2}") # output: abc3
在您的prog_info函數中,您的打印語句實際上並不需要 F-Strings 但是,諸如此類的行需要使用 F-Strings,因為您需要替換以下值:
print("You have ${bank} in your bank.")
print('Die Roll #1 was {roll}')
游戲循環
如果不滿足任何條件,您可以使用 if 語句在里面實現 while True 循環以退出
bank = 500 # Initial Amount
while True:
# Ask user for their bet/guess
print(f'You have ${bank} in your account')
print(f'Choose a number between 2-12')
guess = get_guess()
if guess == 0:
break # This stops the loop
# Roll Dice, Add/Subtract from their bank account, etc.
# Check if their bank account is above 0 after potentially subtracting from their account
if bank <= 0:
break
else:
print("Thanks for playing") # Game has ended
如果對total_bank
函數和主循環進行一些更改,就會得到想要的結果。
試試這個代碼:
import random
def rollDice():
die1 = random.randint(1,6)
die2 = random.randint(1,6)
x = int(die1 + die2)
print('Roll', x)
return x
def prog_info():
print("My Dice Game .v02")
print("You have three rolls of the dice to match a number you select.")
print("Good Luck!!")
print("---------------------------------------------------------------")
print(f'You will win 2 times your wager if you guess on the 1st roll.')
print(f'You will win 1 1/2 times your wager if you guess on the 2nd roll.')
print(f'You can win your wager if you guess on the 3rd roll.')
print("---------------------------------------------------------------")
def total_bank(bank):
bet = 0
while bet <= 0 or bet > min([500,bank]):
print(f"You have ${bank} in your bank.")
a = input("Enter your bet (or Q to quit): ")
if a == 'q': exit()
bet = int(a)
return bank,bet
def get_guess():
guess = 0
while (guess < 2 or guess > 12):
try:
guess = int(input("Choose a number between 2 and 12: "))
except ValueError:
guess = 0
return guess
prog_info()
bank = 500
while True:
bank,bet = total_bank(bank)
guess = get_guess()
if guess == rollDice():
bank += bet
elif guess == rollDice():
bank += bet * .5
elif guess == rollDice():
bank = bank
else:
bank = bank - bet
print(f'You have ${bank} in your bank.')
print(f'Thanks for playing!\n')
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.