如何实现for循环

时间:2013-11-29 16:13:50

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

我有我的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")

4 个答案:

答案 0 :(得分:2)

因此,您希望强制用户重复输入移动,直到他们输入有效移动。这意味着您需要将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"

答案 1 :(得分: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语句。

答案 2 :(得分:1)

在这种情况下,是否使用字典更简单?

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.") 

答案 3 :(得分:0)

建议像这样重写:

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