0

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.

Alexander L. Hayes
  • 3,892
  • 4
  • 13
  • 34
RMK PBR
  • 11
  • 1
  • 1
    Welcome to Stack Overflow! It'd help to make a [mre]. This code as posted is not valid since the loop is missing and `choice` is undefined. Plus, `fromplace` and `toplace` are unused, and there's an unrelated problem if the user enters `e` or `E`. For more tips, check out [ask]. – wjandrea Dec 04 '22 at 01:12
  • Also, we're not here to do your debugging for you. I see **two separate issues** that cause this problem. If your teacher gave you any debugging resources, this'd be a good time to go over them. Or you could check out [How to debug small programs](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/) by Eric Lippert and [How to step through Python code to help debug issues?](/q/4929251/4518341). I recommend Python Tutor for beginners, or if you're already using an IDE with a GUI debugger, you could try that. – wjandrea Dec 04 '22 at 01:18
  • 2
    Relevant: [Is making in-place operations return the object a bad idea?](/q/13062423/4518341) – wjandrea Dec 04 '22 at 01:18
  • I’m sorry @wjandrea I’m very new to python so I don't know what any of this means. all I was wondering is if there was any way to turn a list changed by the user input to turn into the list used for the next change. this all happens within a separate-to-this while loop. the e and E are part of a separate task. apologies for not removing them. – RMK PBR Dec 04 '22 at 02:25
  • The loop is not separate; your question is about something that happens in the loop. – wjandrea Dec 04 '22 at 03:23

1 Answers1

0

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.

  • hello, I managed to get the code to change by adding another while loop after the position and frogs and toads, where the lilypad prints as swappositions(frogsandtoads, pos1-1, pos2-1). I’m still struggling on how to make it so the frogs can only move left and toads to the right and vice versa will be invalid and give an error statement. if you could tell me how to tackle this, I would be very grateful. Thanks. – RMK PBR Dec 04 '22 at 18:37