我对使用Python并不陌生,并且正在努力创建这个骰子掷骰游戏。我在上面度过了愉快的一周,但仍然因无法使其正常/充分工作而感到沮丧。基本上,我很难正确地打印它并继续询问用户的骰子掷骰(2到12之间)。 我预先感谢任何人的帮助和建议!
答案 0 :(得分: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
关键字。尽管它是not advised to use it,但我认为在这种情况下还可以。您实际上并没有更改银行金额-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()
希望这能回答您的问题!
答案 1 :(得分:0)
'F'字符串
F字符串用表达式的值替换变括号{}中的任何内容,例如函数的返回值或任何变量/值:
print(f"abc{1+2}") # output: abc3
在您的 prog_info 函数中,您的打印语句实际上不需要F-String,但是诸如此类的行需要使用F-String,因为您需要替换以下值:
print("You have ${bank} in your bank.")
print('Die Roll #1 was {roll}')
游戏循环
如果不满足您的任何条件,则可以使用if语句实现一会儿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
答案 2 :(得分:-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')