简体   繁体   中英

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! Let's say the user already picked "a1" and picks it again. 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:

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:

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:

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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