2016-05-17 44 views
0

我在Python 3.4上做了一个程序,目的是在给定的足球队中进行随机抽签。不同的ifs在Python 3.4中总是得到相同的代码

import random 

modo = str(input("Sorteo o Simulación completa? ")) 
torneo = str(input("Torneo elegido: ")) 
fase = int(input("Número de equipos (en la fase a sortear): ")) 

def sorteo(x,y): 
    if x == "Champions" or "champions" or "shempions" or "Shempions" or "Uefa Champions League" and y == 32: 
     bombo1 = str(input("8 equipos del primer bombo: ")).split(",") 
     bombo2 = str(input("8 equipos del segundo bombo: ")).split(",") 
     bombo3 = str(input("8 equipos del tercer bombo: ")).split(",") 
     bombo4 = str(input("8 equipos del cuarto bombo: ")).split(",") 
     names = ["A","B","C","D","E","F","G","H"] 
     for i in range(8): 
      grupo = [] 
      n = names[i] 
      first = random.choice(bombo1) 
      grupo.append(first) 
      bombo1.remove(first) 
      second = random.choice(bombo2) 
      grupo.append(second) 
      bombo2.remove(second) 
      third = random.choice(bombo3) 
      grupo.append(third) 
      bombo3.remove(third) 
      fourth = random.choice(bombo4) 
      grupo.append(fourth) 
      bombo4.remove(fourth) 
      print("Grupo " + str(n) + ": " + str(grupo)) 
     return 

if x == "Champions" or "champions" or "shempions" or "Shempions" or "Uefa Champions League" and y == 16: 
     primeros = str(input("8 equipos que quedaron primeros: ")).split(",") 
     segundos = str(input("8 equipos que quedaron segundos: ")).split(",") 
     for i in range(8): 
      n = i + 1 
      first = random.choice(primeros) 
      primeros.remove(first) 
      second = random.choice(segundos) 
      segundos.remove(second) 
      print("Cruce " + str(n) + ": " + str(first) + " VS " + str(second)) 
     return 

if x == "Champions" or "champions" or "shempions" or "Shempions" or "Uefa Champions League" and y == 8: 
     equipos_cuartos = str(input("8 equipos que pasaron a Cuartos: ")).split(",") 
     for i in range(4): 
      n = i + 1 
      first = random.choice(equipos_cuartos) 
      equipos_cuartos.remove(first) 
      second = random.choice(equipos_cuartos) 
      equipos_cuartos.remove(second) 
      print("Cruce " + str(n) + ": " + str(first) + " VS " + str(second)) 
     return 

if x == "Champions" or "champions" or "shempions" or "Shempions" or "Uefa Champions League" and y == 4: 
     equipos_semis = str(input("4 equipos que pasaron a Semis: ")).split(",") 
     for i in range(2): 
      n = i + 1 
      first = random.choice(equipos_semis) 
      equipos_semis.remove(first) 
      second = random.choice(equipos_semis) 
      equipos_semis.remove(second) 
      print("Cruce " + str(n) + ": " + str(first) + " VS " + str(second)) 
     return 

if modo == "Sorteo" or "sorteo": 
    sorteo(torneo,fase) 

但是,结果总是相同的:无论我在“FASE”选择球队的量:它总是问我bombo1,2,3和4支球队,就像我始终选择32队参加。如果我在这里问这是因为我昨天和今天整个疯狂的疯狂,以便修复这个问题,因为当我引入多个比赛和功能也出现这个问题。

+2

可能的复制[如何测试对多值一个变量?(http://stackoverflow.com/questions/15112125/how-do-i-test-one-variable-against-multiple-values) –

+0

当你做'x =='冠军'或'冠军'时'它不同于'如果x ==冠军“或x ==“冠军”,你需要明确地检查每一个“x”或使用'in'运算符。 –

回答

0

问题出在python分组and y == #,它只会将它与最后一个条件分组,所以在第一个if的情况下,它会将它与or "Uefa Champions League" and y == 32分组。然而,正如其他人在评论中所说,它将永远不会像您检查or "champions"那样,这是True,因为任何非空的""字符串都被认为是True。因此,您需要明确检查所有的值,并对x进行分组,以便您的y == #也将检查何时其中任一条件是True

至于建议,我会使用类似以下内容:

if x in {"Champions", "champions", "shempions", "Shempions", "Uefa Champions League"} and y == #:

相关问题