简体   繁体   English

Python赌博骰子游戏?

[英]Python gambling dice game?

I am new to lerning Python and am struggling with creating this dice roll gambling game.我是学习 Python 的新手,并且正在努力创建这个掷骰子赌博游戏。 I have spent a good week on it, and am still just frustrated with not being able to get it to work properly/fully.我已经在它上面度过了愉快的一周,但仍然对无法使其正常/完全工作感到沮丧。 Basically I am having a hard tim egetting it to print properly and continue to ask for the user's dice roll guess (between 2 to 12).基本上我很难让它正确打印并继续询问用户的掷骰子猜测(2到12之间)。 I thank anyone in advance for their help and suggestions!我提前感谢任何人的帮助和建议!

You're total_bank() method may be causing the trouble.您的total_bank()方法可能会导致问题。

def total_bank():
    bank = 500
    while bank > 0:
        print("You have ${bank} in your bank.")
        bet = int(input("Enter your bet: "))

When you call this function, it'll tell you how much money you have in your bank, and once you enter in your bet, it'll do it again because bank is still >0.当你调用这个函数时,它会告诉你你的银行有多少钱,一旦你输入你的赌注,它会再次这样做,因为银行仍然 >0。 So, you'll just be stuck in this infinite loop.所以,你只会被困在这个无限循环中。

ALSO, you'll need a lot of global keywords in this case.此外,在这种情况下,您将需要大量global关键字。 Although it is not advised to use it , I think it's fine in this case.虽然不建议使用它,但我认为在这种情况下很好。 You're not actually changing the bank amount - that bank variable is a local variable, meaning you can't access it outside the total_bank function.您实际上并未更改银行金额 - 该bank变量是一个局部变量,这意味着您无法在total_bank函数之外访问它。 You'll have to return bank every time you call it.每次给bank打电话都得还钱。

So, the code should be所以,代码应该是

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

However, it may not be right for what your purposes are.但是,它可能不适合您的目的。 For example, you can look limit the bet, etc. I'm just simplifying it.例如,您可以查看限制赌注等。我只是在简化它。

Something like this is what you REALLY might want, but I'm not sure.像这样的东西可能是你真正想要的,但我不确定。

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()

Hope this answers your question!希望这能回答你的问题!

'F' Strings 'F' 字符串

F-strings replace whatever is in the squiggly brackets {} with the value of an expression such as a return value from a function or just any variable/value: F 字符串将花括号 {} 中的任何内容替换为表达式的值,例如函数的返回值或任何变量/值:

print(f"abc{1+2}") # output: abc3

In your prog_info function, your print statements don't actually need F-Strings however, lines such as these need to use F-Strings since you need to substitute values in:在您的prog_info函数中,您的打印语句实际上并不需要 F-Strings 但是,诸如此类的行需要使用 F-Strings,因为您需要替换以下值:

print("You have ${bank} in your bank.")
print('Die Roll #1 was {roll}')

Game Loop游戏循环

You could implement a while True loop with and if statement inside to exit if any of your conditions are not met如果不满足任何条件,您可以使用 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

If you make some changes to the total_bank function and the main loop, you will get the results you want.如果对total_bank函数和主循环进行一些更改,就会得到想要的结果。

Try this code:试试这个代码:

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM