0

To be more specific been trying to code a frog and toad game. Most of the code works however can not seem to get python to display when the user has won the game. Feel free to ask for any added information.

frogandtoad= ['f','f','f',' ', 't','t','t']

def game_won():
    for i in range(0,3):            
        if frogandtoad[i] =='f':            
            frog = True
        if frogandtoad[i]=='t':
            toad = True
    if frog == True and toad == True:          
        return True

def playgame(frogandtoad):

    print("From: ")
    from_pos = int(input()) - 1
    print("To: ")
    to_pos = int(input())-1                        #Bring in valid_move
    valid = valid_move(frogandtoad, from_pos, to_pos)      
    if valid:                                       
        value = frogandtoad[from_pos]
        frogandtoad[to_pos] = value
        frogandtoad[from_pos] = ' '
        print("frogandtoad panel", frogandtoad)
    else:
        print("Invalid Move, Look at rules under demonstration!")

    if game_won():
            print("you won")
            exit_game()
quamrana
  • 37,849
  • 12
  • 53
  • 71
Radar
  • 17
  • 5

2 Answers2

2

if game_won==True: should probably be if game_won()==True:

You need to actually call the function game_won.

What does it mean to "call" a function in Python?


Also, as @quamrana points out, for if/while statements, the Pythonic way is to write if condition instead of if condition == True.

kgf3JfUtW
  • 13,702
  • 10
  • 57
  • 80
  • 1
    or if game_won(): – quamrana Dec 09 '17 at 22:10
  • Sorry my mistake have updated the post however code still seems to not work. @sam – Radar Dec 09 '17 at 22:30
  • Could you post more code? For example, we need to know know what your `frogandtoad` variable is, and how you are running the program. – kgf3JfUtW Dec 09 '17 at 22:34
  • Hope it helps, just to be more specific the game requires user to move the letter f (frogs) to the right whilst moving the t(toads) to the left. Once this is achieved should display 'you won' message. @sam – Radar Dec 09 '17 at 22:49
  • Sorry my mistake have updated code but still not seem to be working @quamrana – Radar Dec 09 '17 at 23:29
1

In game_won(), when frog == True and toad == True is False, then one of those variables are going to be undefined (as I get an exception). Unless they are already defined in the global scope.

My consideration for game_one() is:

def game_won():
    result = [False, False]
    for i in range(0,3):
        if frogandtoad[i] =='f':
            result[0] = True
        elif frogandtoad[i]=='t':
            result[1] = True
    return all(result)

Variable result is defined with False and False for frog and toad status. For all(result) to be True, then both items must be True and thus True will be returned else False otherwise.

Edit: I should have mentioned that I ran your code with valid = True as I do not have valid_move() and I commented exit_game() as I do not have that.

michael_heath
  • 5,262
  • 2
  • 12
  • 22
  • Ignore previous comment. Change range and worked perfectly. Thanks for the help, much appreciated! @michael_heath – Radar Dec 10 '17 at 09:59