2017-09-28 6 views
1

파이썬에서 내 목록을 이해하는 데 문제가 있습니다. 나는 내가 찾고있는 3 가지 조건을 가지고 있으며 그 중 두 가지를하는 법을 알고 있지만 조건 중 하나가 올바르게 작동하지 않는 것 같습니다. 내 목록에있는 모든 숫자가 동일하고 그들은 모두 특정 수의 경우 내 목록의 모든 숫자가 같은 경우조건이 파이썬에서 일치하는지 확인하십시오.

  1. 는, 다음 점을
  2. 을 추가하지만 그들은 수행

    내 조건은 특정 숫자와 같지 않다면 다른 것을하십시오.

  3. 목록의 숫자가 일치하지 않지만 다른 숫자보다 특정한 숫자와 일치하는 경우.

나는 1 개의 작업을 수행하고 있으며, 3을 수행하는 방법을 알고 있지만 제대로 작동하지 못합니다. 내가 목록에 넣은 숫자가 무엇이든 관계없이이 조건은 여전히 ​​참과 일치합니다. 누군가가 도와 주실 수 있습니까?

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 

OUTPUT :

이 같은
Starting Round Number 1 
You rolled: [2, 3, 3] 
MINI-BUNCO! 
Points this round: 5 
+0

모든 숫자가 첫 번째 숫자와 같지만 ROUND_NUMBER와 같지 않은 경우 어떻게됩니까? – jonrsharpe

+0

힌트 : 먼저'rolls '의 모든 숫자가'rolls [0]'과 동일한 지 확인하고, 맞으면'rolls [0] == ROUND_NUMBER'를 테스트하십시오. –

+0

[여기] (https://stackoverflow.com/questions/3844801/check-if-all-elements-in-a-list-are-identical)에 제안 된 것을 사용하고 @ PM2Ring에서 제안한 것처럼 롤스 [0 ]! = ROUND_NUMBER – ShreyasG

답변

1

뭔가가 당신을 얻어야한다 ...

rolls = [5,5,5,5,5,5] 

specificNum = 6 

if len(set(rolls)) == 1: 
    if rolls[0] != specificNum: 
     print 'Do something' 
0
#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!') 

만약 내가 제대로 이해하고,이 제공해야합니다 여기에 내 현재 코드입니다 당신이 필요로하는 것! 이것이 당신이 원하는 것이 아니라면, plz는 나를 downvote하지 마십시오! 나는 이해하기가 열심히 노력했다.