我是学习 Python 的新手,正在努力创建这个掷骰子赌博游戏。我已经度过了美好的一周,但仍然对无法使其正常/完全工作感到沮丧。基本上,我很难让它正确打印并继续询问用户的掷骰子猜测(2 到 12 之间)。我提前感谢任何人的帮助和建议!
2477 次
3 回答
1
你的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()
希望这能回答你的问题!
于 2020-10-31T01:21:18.310 回答
0
'F' 字符串
F-strings 将波浪括号 {} 中的任何内容替换为表达式的值,例如函数的返回值或任何变量/值:
print(f"abc{1+2}") # output: abc3
在您的prog_info函数中,您的 print 语句实际上并不需要 F 字符串,但是,诸如这些行需要使用 F 字符串,因为您需要替换以下值:
print("You have ${bank} in your bank.")
print('Die Roll #1 was {roll}')
游戏循环
如果您的任何条件不满足,您可以在内部实现一个带有 and 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
于 2020-10-31T01:27:30.483 回答
-1
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')
于 2020-10-31T01:20:30.673 回答