Python彩票游戏

时间:2014-04-17 19:39:08

标签: python python-2.7

这是我第一个完全靠自己完成的项目之一,没有任何教程,我确信它有点草率。现在,我无法弄清楚为什么我的付款总是作为一个'框的支出而输出。打赌。我还没有完全弄清楚如何用5位数字代表盒子类型的赌注,所以程序的一部分还没有写出来。有关修复betType的任何帮助或建议吗?

#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 个答案:

答案 0 :(得分: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这些在一起,你将永远变为现实。你必须像第二种方法一样写出每个字符串的完整比较。

答案 1 :(得分:0)

Python比较的行为方式不符合您的想法:

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

相当于

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

'S'是一个非零实体,因此它的计算结果为True - 您可以尝试:

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

作为对其余代码的一般性评论:您不需要将播放器使用的位数编码为字符串值 - 如果self.digits是数字,则更好,因为它允许更多Pythonic表达式 - 看看您是否可以根据choice的值动态更改digits,而不使用单独的if语句。 (作为提示,调查“列表理解”)

快乐的编码!