简体   繁体   中英

When I swap two values in my list it is not a permanent swap in the list?

I'm new to Python, and I have an assignment where I have a frogs vs. toads game. They are in a list and they need to swap places one step at a time. the list looks like this: ["F","F","F"," ","T","T","T"] and should look like ["T","T","T"," ","F","F","F"] to win the game. The user inputs From and To and they swap. But my code is not taking the swapped code as the new code when the new From and To are being entered. How do I fix this?

This is all within a while loop as there are other options at the beginning of the game.

Also, one of the rules for the assignment is that the frogs are only allowed to one one direction to the left and the toads vice versa. if anyone knows how to put that into my code that would be very much appreciated.

Here's my code:

elif choice== 'P':
    position= ["1","2","3","4","5","6","7"]
    frogsandtoads= ["F","F","F"," ","T","T","T"]
    print("Position: ",position)
    print("Lilypad:  ",frogsandtoads)

    def swappositions(frogsandtoads, pos1, pos2):
        if pos1== 'e':
            exit()
        if pos1== 'E':
            exit()
        
        frogsandtoads[pos1], frogsandtoads[pos2] = frogsandtoads[pos2], frogsandtoads[pos1]#the swapping of the positions
        return frogsandtoads
    pos1 = fromplace= int(input("From: "))
    pos2 = toplace= int(input("To: "))
    
    print(swappositions(frogsandtoads, pos1-1, pos2-1))         
    frogsandtoads=swappositions(frogsandtoads, pos1-1, pos2-1)
    if frogsandtoads== ["T","T","T"," ","F","F","F"]: #this is what is not working
       break

this is my outcome when I run the code:

Please choose an option: p
Position:  ['1', '2', '3', '4', '5', '6', '7']
Lilypad:   ['F', 'F', 'F', ' ', 'T', 'T', 'T']
From: 3
To: 4
['F', 'F', ' ', 'F', 'T', 'T', 'T']
Position:  ['1', '2', '3', '4', '5', '6', '7']
Lilypad:   ['F', 'F', 'F', ' ', 'T', 'T', 'T']
From: 

As you can see I don't know how to make it so the lilypad changes with the input of the from and to the second time round.

So you want it such that the lilypad list does not revert back to ['F', 'F', 'F', ' ', 'T', 'T', 'T'] and that the position list also won't revert back to ['1', '2', '3', '4', '5', '6', '7'] ?

The problem in your code is that you reset the variables here:

    position= ["1","2","3","4","5","6","7"]
    frogsandtoads= ["F","F","F"," ","T","T","T"]

If you do not want those lists to get reset to that then you should move those two lines out of your game loop aka outside of your while loop. This should fix the problem. You also have the stepping problem such that the frogs can only go to the left and the toads to the right, but I believe that you can implement this yourself. Feel free to ask questions.

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