【问题标题】:How to implement a for loop如何实现for循环
【发布时间】:2013-11-29 16:13:49
【问题描述】:

我已经设置了我的 for 循环,但我缺少一个条件,只是不知道把它放在哪里!假设用户已经选择了"a1" 并再次选择它。我不希望使用该值,而是告诉他它已经被选中并让他再次选择。我试着做,但按照我的方式,它告诉他他已经选好了,但没有让他再走。

def inputCoordinate():
    coordinate = False 
    while not coordinate :
        user = (input("Enter your move: "))
        if user in List:
            if user == "a1":
                value = "O"
                gameboard[0] = value 
                playedmoves.append("a1")
            elif user == "a2":
                value = "O"
                gameboard[3] = value
                playedmoves.append("a2")
            elif user == "a3":
                value = "O"
                gameboard [6] = value
                playedmoves.append("a3")
            elif user == "b1":
                value = "O"
                gameboard[1] = value
                playedmoves.append("b1")
            elif user =="b2":
                value = "O"
                gameboard[4] = value
                playedmoves.append("b2")
            elif user == "b3":
                value = "O"
                gameboard[7] = value
                playedmoves.append("b3")
            elif user =="c1":
                value = "O"
                gameboard[2]=value
                playedmoves.append("c1")
            elif user == "c2":
                value = "O"
                gameboard[5] = value  
                playedmoves.append("c2")
            elif user == ("c3"):
                value = "O"
                gameboard[8] = value 
                playedmoves.append("c3")

        else:
            print("invalid Coordinates")
            continue 
        return value

playedmoves =("a1","b2")
List =  ("a1", "a2", "a3", "b1", "b2", "b3", "c1", "c2", "c3")

【问题讨论】:

    标签: python function for-loop python-3.x


    【解决方案1】:

    所以你想强制用户重复输入一个动作,直到他们输入一个有效的动作。这意味着您需要将input 语句包装在一个循环中,如下所示:

    while some_condition_is_not_met:
        user = input("Enter your move: ")
        if not valid_move(user):
            print "bad move, please re-enter"
    

    您可以使while 循环依赖于在用户输入有效移动时设置的变量:

    good_move = False
    while not good_move:
        user = input("Enter your move: ")
        if valid_move(user):
            good_move = True
        else:
            print "bad move, please re-enter"
    

    【讨论】:

      【解决方案2】:

      只需在您正在测试移动是否有效的地方测试playedmoves

      if user in List and user not in playedmoves:
      

      您真的想在这里使用映射将位置转换为索引:

      pos_to_index = {"a1": 0, "a2": 3, "a3": 6, "b1": 1, "b2": 4, "b3": 7, "c1": 2, "c2": 5, "c3": 8}
      
      def inputCoordinate():
          while True:
              user = (input("Enter your move: "))
              index = pos_to_index.get(user)
              if index is not None and gameboard[index] not in ('O', 'X'):
                  gameboard[index] = 'O'
              else:
                  print("invalid Coordinates")
                  continue 
              return 'O'
      

      在这里,我们使用游戏板来查看是否仍然可以移动;如果那里已经有零或交叉,那么这一步显然是无效的。 pos_to_index 映射一次性摆脱了 9 个 if 语句。

      【讨论】:

        【解决方案3】:

        在这种情况下使用字典不是更简单吗?

        playedmoves = []
        moves = {"a1":0, "a2":3, "a3":6, "b1":2, "b2":4, "b3":7, "c1":2, "c2":5, "c3":8}
        
        if user in moves and not in playedmoves:
            gameboard[moves[user]] = "0"
            playedmoves.append(user)
        else:
            print("Invalid coordinates.") 
        

        【讨论】:

          【解决方案4】:

          建议改写成这样:

          mapping = {"a1": 0, "a2": 3, ... }
          List = mapping.keys()
          playedmoves =("a1","b2")
          
          def inputCoordinate():
              coordinate = False 
              while not coordinate :
                  user = (input("Enter your move: "))
                  value = "0"  # <---
                  if user in List:
                      gameboard[mapping[user]] = value
                      playedmoves.append(user)
          
                  else:
                      print("invalid Coordinates")
                      continue 
                  return value
          

          【讨论】:

            猜你喜欢
            • 2021-10-28
            • 2014-02-15
            • 2023-01-10
            • 2021-09-12
            • 1970-01-01
            • 2012-04-16
            • 2020-02-04
            • 2021-09-26
            • 1970-01-01
            相关资源
            最近更新 更多