[英]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.