如何在一个函数中连续创建多个 if 语句,以累积地添加到变量中?

问题描述 投票:0回答:1

我正在尝试创建一台免费的老虎机来学习Python,它工作正常,直到我意识到线路支付将独立于一个和另一个运行,相反,它们会检查一个,然后检查 if 语句的下一部分直到达到相关的目标。相反,我想构建逻辑来检查每个变体,无论上一行或下一行是否为真。

目前,我尝试执行此操作的方法是使用独立的 if 语句为机器的每个“行”创建逻辑,如下所示:

def printResult1():
    '''
    prints the result for the first symbol
    '''
    global stake, firstReelBottom, firstReelMiddle, firstReelTop, secondReelBottom, secondReelMiddle, secondReelTop, thirdReelBottom, thirdReelMiddle, thirdReelTop, fourthReelBottom, fourthReelTop, fourthReelMiddle, fifthReelBottom, fifthReelMiddle, fifthReelTop, result 
  #symbol1 checking, line 1,2,3,
    if((firstReelTop == "symbol1" or firstReelTop == "wild") and (secondReelTop == "symbol1" or secondReelTop == "wild") and (thirdReelTop == "symbol1" or thirdReelTop == "wild") and (fourthReelTop != "symbol1" and fourthReelTop != "wild")):
        result += 3
    if((firstReelTop == "symbol1" or firstReelTop == "wild") and (secondReelTop == "symbol1" or secondReelTop == "wild") and (thirdReelMiddle == "symbol1" or thirdReelMiddle == "wild") and (fourthReelTop != "symbol1" and fourthReelTop != "wild")):
        result += 3
    if((firstReelTop == "symbol1" or firstReelTop == "wild") and (secondReelTop == "symbol1" or secondReelTop == "wild") and (thirdReelBottom == "symbol1" or thirdReelBottom == "wild") and (fourthReelTop != "symbol1" or fourthReelTop != "wild")):
        result += 3
    if((firstReelTop == "symbol1" or firstReelTop == "wild") and (secondReelTop == "symbol1" or secondReelTop == "wild") and (thirdReelTop == "symbol1" or thirdReelTop == "wild") and (fourthReelTop == "symbol1" or fourthReelTop == "wild") and (fifthReelTop != "symbol1" or fifthReelTop != "wild")):
        result += 30
    if((firstReelTop == "symbol1" or firstReelTop == "wild") and (secondReelTop == "symbol1" or secondReelTop == "wild") and (thirdReelMiddle == "symbol1" or thirdReelMiddle == "wild") and (fourthReelTop == "symbol1" or fourthReelTop == "wild") and (fifthReelTop != "symbol1" or fifthReelTop != "wild")):
        result += 30
    if((firstReelTop == "symbol1" or firstReelTop == "wild") and (secondReelTop == "symbol1" or secondReelTop == "wild") and (thirdReelBottom == "symbol1" or thirdReelBottom == "wild") and (fourthReelTop == "symbol1" or fourthReelTop == "wild") and (fifthReelTop != "symbol1" or fifthReelTop != "wild")):
        result += 30
    if((firstReelTop == "symbol1" or firstReelTop == "wild") and (secondReelTop == "symbol1" or secondReelTop == "wild") and (thirdReelTop == "symbol1" or thirdReelTop == 'wild') and (fourthReelTop == "symbol1" or fourthReelTop == "wild") and (fifthReelTop == "symbol1" or fifthReelTop == "wild")):
        result += 300
    else: 
        result += 0

这以前有效: 我将单个 if 语句转换为多个 if 语句,并且在添加

+= result
而不是
== result
之前。

python python-3.x
1个回答
0
投票

Python 非常灵活。条件可以声明为变量。变量或值可以放置在列表中。所以,这就是解决方案:

lst_1 = [firstReelTop, secondReelTop]
values = ['symbol1', 'wild']
lst_2 = [thirdReelTop, thirdReelMiddle, thirdReelBottom]
lst_3 = [fourthReelTop, ]
cond_1 = any(x in values for x in lst_1)
cond_2 =any[x in values for x in lst_2]
cond_3 = fourthReelTop in values
cond_4  = fifthReelTop in values
if cond_1 and cond_2 and not cond_3:
    result += 3
elif cond_1 and cond_2 and cond_3 and not cond_4:
    result +=30
elif cond_1 and cond_2 and cond_3 and cond_4:
    result += 300
else:
    result += 0
© www.soinside.com 2019 - 2024. All rights reserved.