微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

在Python赌博游戏上进行印刷/数学运算?

如何解决在Python赌博游戏上进行印刷/数学运算?

所以我在输出时再次遇到了这段代码的麻烦。基本上,我需要打印一些关键功能,但是每当我设法使它打印一件东西时,它就完全把其余的打印弄乱了。因此,例如,如果需要,我需要它打印Roll # 1 (1 - 3) was (whatever number)而不是Roll (whatever number)。但是我也需要它最多最多3卷。这是我的第二个问题所在。每当我尝试对其进行编码以在用户不匹配任何掷骰的情况下从银行中减去下注时,它将我的减法算为第四掷骰,并弄乱了数学。因此,从Roll #1#3的{​​{1}} 我的第三个问题是,我需要让程序继续循环直到用户输入Roll #4结束脚本或银行金额达到0 (zero)

解决方法

您应该重新设计程序。首先,您将在以下位置为每次条件检查生成新结果

if guess == rollDice():
        bank = bet * 2
    elif guess == rollDice():
        bank += bet * .5
    elif guess == rollDice():
        bank = bank

您的代码未正确缩进。

   [...]
   elif guess == rollDice():
        bank += bet * .5
    elif guess == rollDice():
        bank = bank
else:
    guess != rollDice()
    bank = bank - bet
    print(f'You have ${bank} in your bank.')
    print(f'Thanks for playing!')

依此类推...


具有模拟单个骰子掷骰的功能,例如:

def roll():
    return random.randint(1,6)

并处理主要功能中的其余部分,例如:

prog_info()
while True: #main loop
    rolls = list() #redefines after each loop
    score = 2
    for i in range(3): #3 dice roll
        bank,bet = total_bank(bank)
        guess = get_guess()
        if not guess: #exit condition
            break
        rolls.append(roll())
        if sum(rolls) == guess:
            bank = bet * score
            break #break on match
        score = score - 0.5 #after each roll we have less money to win
    print(f'You have ${bank} in your bank.')
    print(f'Thanks for playing!')
        
    
,

进行几次更改即可获得所需的结果

  • 将滚动计数传递到rollDice函数
  • 在if块的底部添加一个else,以检查0 bank

这是更新的代码:

import random

def rollDice(cnt):
    die1 = random.randint(1,6)
    die2 = random.randint(1,6)
    x = int(die1 + die2)
    print('Roll #',cnt,'was',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.')
        get_bet = input('Enter your bet (or 0 to quit): ')
        if get_bet == '0': 
            print('Thanks for playing!')
            exit()
        bet = int(get_bet)
    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
guess = get_guess

while True:
    rcnt = 0
    bank,bet = total_bank(bank)
    guess = get_guess()
    if guess == rollDice(rcnt+1):
        bank += bet * 2
    elif guess == rollDice(rcnt+2):
        bank += bet * .5
    elif guess == rollDice(rcnt+3):
        bank = bank
    else:
        bank = bank - bet  # no match
        if bank == 0: 
           print('You have no money left. Thanks for playing!')
           exit()

输出

You have $500 in your bank.
Enter your bet (or 0 to quit): 500
Choose a number between 2 and 12: 4
Roll # 1 was 11
Roll # 2 was 6
Roll # 3 was 7
You have no money left. Thanks for playing!

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?