我的最后两个elif/else语句无法执行,我无法找到原因?

2024-09-24 06:23:50 发布

您现在位置:Python中文网/ 问答频道 /正文

我对下面代码中的my last elif和else语句有一个相当简单的问题。。。他们根本不会执行。我是python的高手,所以很可能是个简单的错误:/ 控制台不显示任何语法错误或任何。。。它执行代码,但结果错误

            starthandvalue = 0

            if splitfirstcard[0] == splitsecondcard[0]:
                ispar = True
                
                if (splitfirstcard[0] and splitsecondcard[0] == 'A') or (splitfirstcard[0] and splitsecondcard[0]== 'K') or (splitfirstcard[0] and splitsecondcard[0] == 'Q'):
                    starthandvalue =+83
                    
                elif (splitfirstcard[0] and splitsecondcard[0] == 'J') or (splitfirstcard[0] and splitsecondcard[0] == '10') or (splitfirstcard[0] and splitsecondcard[0] == '9'):
                    starthandvalue =+75

                elif (splitfirstcard[0] and splitsecondcard[0] == '8') or (splitfirstcard[0] and splitsecondcard[0] == '7') or (splitfirstcard[0] and splitsecondcard[0] == '6') or (splitfirstcard[0] and splitsecondcard[0] == '5'):
                    starthandvalue ==+62

                else:
                    starthandvalue ==+55

输出

['Q', 's'] ['Q', 'h']
 Hand value: about 83%
['9', 's'] ['9', 'h']
 Hand value: about 75%
['6', 's'] ['6', 'h']
 Hand value: about 0%  <--- this should be 62
['2', 's'] ['2', 'h']
 Hand value: about 0%  <--- this should be 55
['3', 's'] ['3', 'd']
 Hand value: about 0%  <--- this should be 55
['K', 's'] ['K', 'd']
 Hand value: about 83%
```

Tags: orand代码value错误bethiselse
2条回答

快速一瞥,splitfirstcard[0] and splitsecondcard[0] == '5'并不是你想的那样。这些语句应该是splitfirstcard[0] == 5 and splitsecondcard[0] == '5'

例如:

t1 = 'a'
t2 = 'b'
t1 and t2 == 'b'
Out: True

t1 == 'b' and t2 == 'b'
Out: False

将诸如=+==+之类的运算符更改为+=

相关问题 更多 >