简体   繁体   English

如何实现for循环

[英]How to implement a for loop

I have my for loop set up, but im missing one condition and just don't know where to put it! 我已经设置了for循环,但是我错过了一个条件,只是不知道放在哪里! Let's say the user already picked "a1" and picks it again. 假设用户已经选择"a1"并再次选择它。 I don't want that value to be used but instead tell him it's already been picked and let him pick again. 我不希望使用该值,而是告诉他已经被选择,让他再次选择。 I tried making it but the way I had it, it told him that he already picked it, but didn't let him go again. 我尝试过制作它,但是我拥有它的方式告诉他他已经选择了它,但是没有让他再去。

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

So you want to force the user to repeatedly enter a move until they enter a valid one. 因此,您想强制用户重复输入一个动作,直到他们输入有效的动作为止。 That means that you need to wrap the input statement in a loop, something like this: 这意味着您需要将input语句包装在一个循环中,如下所示:

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

You could make the while loop depend on a variable that gets set when the user enters a valid move: 您可以使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"

Just test against playedmoves right where you are testing if the move is valid: 只需在正在测试移动是否有效的地方对已playedmoves的移动进行测试:

if user in List and user not in playedmoves:

You really want to use a mapping here to translate position to index: 您确实要在此处使用映射将位置转换为索引:

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'

Here we use the gameboard to see if a move is still available; 在这里,我们使用游戏板来查看是否仍可进行移动。 if there is a naught or cross there already, the move is obviously not a valid one. 如果已经不存在任何交叉,那么此举显然是无效的。 The pos_to_index mapping gets rid of the 9 if statements in one go. pos_to_index映射pos_to_index消除了9个if语句。

Isn't using a Dictionary in this case waaaay simplier? 在这种情况下不使用字典更简单吗?

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

Suggest to rewrite like this: 建议像这样重写:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM