Python 彩票游戏

-2 投票
2 回答
1964 浏览
提问于 2025-04-18 03:10

这是我第一次完全自己做的项目,没有参考任何教程,我知道做得有点粗糙。目前,我搞不清楚为什么我的奖金总是显示为“盒子”类型的投注奖金。我也还没完全弄明白如何表示5位数字的盒子类型投注,所以那部分程序还没写好。如果有人能帮我解决这个投注类型的问题,我会很感激。

#lotto program

#Created 04/14/2014, last modified 04/16/14
#4/16/14 made lottery a class, added 4 or 5 digit support
#4/16/14 added support for box type bets, now program assumes all payouts are for box type bets --UNRESOLVED
from random import *
import time



class Lottery:
def __init__(self, digits, betType, betAmt):
    self.digits = digits
    self.betType = betType
    self.betAmt = betAmt

def winner(self, tries, choice, betType):
    if self.betType.upper() == 'STRAIGHT' or 'S':
        if self.digits == '3':
            if self.betAmt == '1.00':
                payout = 500
            elif self.betAmt == '0.50':
                payout = 250
        elif self.digits == '4':
            if self.betAmt == '1.00':
                payout = 5000
            elif self.betAmt == '0.50':
                payout = 2500
        elif self.digits == '5':
            if self.betAmt == '1.00':
                payout = 50000
            elif self.betAmt == '0.50':
                payout = 25000

#At present I have not figured out how to add payouts for box type bets -- 4/16/2014
    if self.betType.upper() == 'BOX' or 'BOXED' or 'B':
        if self.digits == '3':
            if self.betAmt == '1.00':
                if len(set(choice)) == 3: # 6 way box
                    payout = 83
                elif len(set(choice)) == 2: # 3 way box
                    payout = 167
            elif self.betAmt == '0.50': 
                if len(set(choice)) == 3: # 6 way box
                    payout = 41.5
                elif len(set(choice)) == 2: # 3 way box
                    payout = 83.5
        elif self.digits == '4':
            if self.betAmt == '1.00':
                if len(set(choice)) == 4: #24 way box
                    payout = 200
                elif len(set(choice)) == 3: #12 way box
                    payout = 400
                elif len(set(choice)) == 2:
                    if sorted(choice)[0] == sorted(choice)[1] == sorted(choice)[2]: # 4 way box
                        payout = 1198
                    else: # 6 way box
                        payout = 800 
            elif self.betAmt == '0.50':
                if len(set(choice)) == 4: #24 way box
                    payout = 100
                elif len(set(choice)) == 3: #12 way box
                    payout = 200
                elif len(set(choice)) == 2:
                    if sorted(choice)[0] == sorted(choice)[1] == sorted(choice)[2]: # 4 way box
                        payout = 599
                    else: # 6 way box
                        payout = 400
        elif self.digits == '5':
            if self.betAmt == '1.00':
                if len(set(choice)) == 5: # 120 way box
                    payout = 417
                elif len(set(choice)) == 4: # 60 way box
                    payout = 834
                elif len(set(choice)) == 3: # Still working on all 5 digit box payouts
                    if sorted(choice)[0] == sorted(choice)[1]:
                        pass
            elif self.betAmt == '0.50':
                payout = 25000

    print "-----" * 10
    print "winner after " + str(tries) + " tries!"
    print "It took you " + str(tries/2) + " days to win!"
    print "your tickets cost $" + str(tries * float(self.betAmt)) + ".00"
    print "Your payout was $" + str(payout) + ".00"
    print "Your Net Revenue was $" + str(payout - tries) + ".00"
    print "-----" * 10


def play(self):
    print "<>" * 40
    print "Use 'mp' to play the same 'machine pick' each time"
    print "Use 'ap' to play a new 'machine pick' number each time"
    print "<>" * 40
    guess = raw_input("Choose a lottery number and see how long it takes until you win! >>")
    if guess.upper() == 'MP': #added machine pick option
        if self.digits == '3': #attempt support for 4 and 5 digit numbers
            choice = str(randint(0,9)) + str(randint(0,9)) + str(randint(0,9))
        elif self.digits == '4':
            choice = str(randint(0,9)) + str(randint(0,9)) + str(randint(0,9)) + str(randint(0,9))
        elif self.digits == '5':
            choice = str(randint(0,9)) + str(randint(0,9)) + str(randint(0,9)) + str(randint(0,9)) + str(randint(0,9))
        else:
            pass
    elif guess.upper() == 'AP': #placeholder for autopick in main loop
        pass
    else:
        choice = guess
    tries = 0
    while True:
        if guess.upper() == 'AP': #added machine pick option
            if self.digits == '3': #attempt support for 4 and 5 digit numbers
                choice = str(randint(0,9)) + str(randint(0,9)) + str(randint(0,9))
            elif self.digits == '4':
                choice = str(randint(0,9)) + str(randint(0,9)) + str(randint(0,9)) + str(randint(0,9))
            elif self.digits == '5':
                choice = str(randint(0,9)) + str(randint(0,9)) + str(randint(0,9)) + str(randint(0,9)) + str(randint(0,9))
        if self.digits == '3': #attempt support for 4 and 5 digit numbers
            winning = str(randint(0,9)) + str(randint(0,9)) + str(randint(0,9))
        elif self.digits == '4':
            winning = str(randint(0,9)) + str(randint(0,9)) + str(randint(0,9)) + str(randint(0,9))
        elif self.digits == '5':
            winning = str(randint(0,9)) + str(randint(0,9)) + str(randint(0,9)) + str(randint(0,9)) + str(randint(0,9))
        print winning, choice
        tries += 1
        if self.digits == '3':
            time.sleep(0.02)
        elif self.digits == '4':
            time.sleep(0.002)
        else:
            time.sleep(0.0005)
        if self.betType.upper() == 'STRAIGHT':
            if winning == choice:
                self.winner(tries, choice, self.betType)
                break
        elif self.betType.upper() == 'BOXED' or 'BOX':
            if sorted(winning) == sorted(choice):
                self.winner(tries, choice, self.betType)
                break
class Menu:
def __init__(self):
    #self.game = Lottery(digits, betType, betAmt)
    self.start()
def start(self):
    print "Welcome to the Lottery!"
    self.digits = raw_input("Would you like to play '3' digit, '4' digit, or '5' digit? >> ")
    self.betType = raw_input("Straight, or Boxed bet type? >> ")
    self.betAmt = raw_input("$0.50, or $1.00? >> ")
    self.game = Lottery(self.digits, self.betType, self.betAmt)
    self.game.play()
    raw_input("Enter to play again")

Menu1 = Menu()
#game = Lottery(digits, betType, betAmt)
if __name__ == '__main__':
while True:
    Menu1.start()

2 个回答

4

这个

if self.betType.upper() == 'BOX' or 'BOXED' or 'B'

不等于

if (self.betType.upper() == 'BOX') or (self.betType.upper() == 'BOXED') or (self.betType.upper() =='B')

第一个会计算三个不同的表达式:

if (self.betType.upper() == 'BOX')   # either True or False
if ('BOXED')  # this will ALWAYS evaluate to True
if ('B')     # this will also always evaluate to True

所以如果你把这些用or连接起来,你总是会得到真。你需要像第二种方法那样,逐个完整比较每个字符串。

0

Python中的比较操作可能和你想的有点不一样:

 if self.betType.upper() == 'STRAIGHT' or 'S':

这相当于

if (self.betType.upper() == 'STRAIGHT') or ('S')

'S'是一个非零的实体,所以它的值是True - 你可以试试看:

if 'S':
    print('huh?')

关于你代码的其他部分,有个一般性的建议:你不需要把玩家使用的数字位数当作字符串来处理 - 如果self.digits是一个数字会更好,这样可以让你的代码更符合Python的风格 - 看看能不能根据digits的值动态改变choice,而不需要使用单独的if语句。(作为提示,可以研究一下“列表推导式”)

祝你编码愉快!

撰写回答