0

I have a text file that follows:

Run
Jump
Jump
Swim
Run

And I have 3 methods, run, jump and swim, how do I run those functions when each of those lines gets read, so it would run "Run" first and that would run the run function then it would run the "Jump" function twice ect, ive been stuck for 2 days and thought a hint would be much appreciated, thanks! ^-^

3
  • Break down the problem and tackle the steps one by one. Do you know how to open a file? Do you know how to read from it line by line? Do you know how to check whether a line is equal to some string? Do you know how to call a function? Right now the question boils down to "please write it for me" which is not what StackOverflow is for. Commented Apr 7, 2021 at 19:14
  • i know how to open a file, i know how to read it line by line, im not sure on the line is equal to string, and i know how to call a function just not using the string i just got from the text file. Commented Apr 7, 2021 at 19:16
  • Then please edit your post to add the code that you've got so far, so we can help you in a more focused way. Commented Apr 7, 2021 at 19:18

2 Answers 2

1

I think you should just have a dictionary that holds commands+functions:

def jump():
   player.y += 10 
    ...

commands = {"jump":jump,"Run":run,"Swim":swim}
for cmd in command_file.split("\n"):
    commands.get(cmd.strip())()

Sign up to request clarification or add additional context in comments.

2 Comments

Curious as to why you use get( ), rather than just [cmd.strip()]: the only difference it will cause is that instead of getting a KeyError if the cmd from the file is invalid (with the invalid key actually in the error message), whereas your code will generate a ValueError: NoneType is not callable Exception which is a lot less useful..
That is True, didn't think about the Errors readibility
1

This is what I would do :

def run():
    print('Running')
def jump():
    print('Jump')
def swim():
    print('Swim')

commands_table = {'Run':run, 'Jump':jump, 'Swim':swim}

with open('commands.txt', 'r') as command_file:
     for cmd in command_file:
         cmd = cmd.strip()
         commands_table[cmd]()

We use a dictionary to store the relationship between the commands in the text file and the functions that need to be executed.

The with statement and beyond opens the text file, reads the commands in, removes any whitespace and then executes the function by extracting it from the dictionary.

4 Comments

The other 'simpler' way of course is just to use a series of if statements but that gets messy as you need to expend the number of commands. Using a dictionary as a jump table is a lot easier.
I got something closed to this yesterday, guess i was on the right track, thanks tho!!
In your code you name both the opened file AND the dictionary "commands", which would cause some problems. ( you should maybe also include the commands.readlines() )
@lightstack - take your point about the messed up names - ty. You don't need readlines() if you are using a for loop on an open file. An open file works like a container of lines, so doing readlines() is redundant.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.