检查python中的条件是否匹配
在用python理解列表时遇到困难。我有三个条件,我正在寻找,我知道如何做其中两个,但其中一个条件似乎不工作的权利 我的条件是:检查python中的条件是否匹配,python,list-comprehension,Python,List Comprehension,在用python理解列表时遇到困难。我有三个条件,我正在寻找,我知道如何做其中两个,但其中一个条件似乎不工作的权利 我的条件是: 如果我的列表中的所有数字都是相同的,并且都是特定的数字,则添加点数 如果我的列表中的所有数字都相同,但它们不等于某个特定的数字,那么请执行其他操作 若列表中的数字不匹配,但它们等于某个特定的数字,则执行其他操作 我有一个在工作,我知道如何做第三个,但我不能让第二个正常工作。无论我在列表(rolls)中输入什么数字,这个条件仍然符合True。有人能帮忙吗?这是我目前的代
def check_conditions(rolls, round_number):
"""
Check if number on rolled die matches one of three conditions
:param rolls:
:param round_number:
:return round:
"""
round_score = ROUND_TOTAL
rolls = str(rolls)
bunco = all(roll == ROUND_NUMBER for roll in rolls)
mini_bunco = all(roll == roll[0] and roll != ROUND_NUMBER for roll in rolls)
if bunco == True:
print("BUNCO!")
round_score += 20
elif mini_bunco == True:
print("MINI-BUNCO!")
round_score += 5
else:
pass
return round_score
输出:
Starting Round Number 1
You rolled: [2, 3, 3]
MINI-BUNCO!
Points this round: 5
像这样的事情应该能让你达到目的
rolls = [5,5,5,5,5,5]
specificNum = 6
if len(set(rolls)) == 1:
if rolls[0] != specificNum:
print 'Do something'
如果我理解正确,这应该给你你需要的!如果这不是你想要的,请不要否决我!我尽了最大的努力去理解。如果所有的数字都等于第一个数字,而不是整数?提示:首先检查
rolls
中的所有数字是否与rolls[0]
相同,如果是真的,然后测试rolls[0]==ROUND\u number
。使用建议的方法,然后按照@PM2Ring建议的方法,检查是否滚动[0]!=ROUND_NUMBERmini_bunco=all(roll==roll[0]和roll!=ROUND_NUMBER for roll-in rolls)
是我现在拥有的,但我得到了一个TypeError:“int”对象不是subscriptableUpdated最初问题中的代码并提供了输出
#imports
import random
#variables
Roll_1_return = False
Roll_2_return = False
round_score = ROUND_TOTAL
#assuming you only want to roll twice
def Rolls():
Roll_1 = random.randrange(1, 10)
Roll_2 = random.randrange(1, 10)
While True:
if Roll_1 == 3:
Roll_1_return = True
return Roll_1_return
break
else:
break
While True:
if Roll_2 == 7:
Roll_2_return = True
return Roll_2_return
break
else:
break
Rolls()
if Roll_1_return == True:
print('Roll 1 is correct!')
round_score + 25
else:
print('Roll 1 is incorrect..')
if Roll_2_return == True:
print('Roll 2 is correct!')
round_score + 25
else:
print('Roll 2 is incorrect..')
if round_score == 50:
print('You won $100!')
elif round_score == 25:
print('You won $50!')
else:
print('Too bad, you lost!')