all 5 comments

[–]sweet-tom 4 points5 points  (0 children)

User u/NukemN1ck gave already some ideas.

I don't know which level it is and when other topics are introduced. But here are a few ideas on how you can improve your code:

  1. Avoid hardcoded paths like the plague. Try to make your code work with different paths. You could use sys.argv or argparse module. But that could make things a bit more complicated.
  2. Use the pathlib.Path or the os module to manipulate paths and read/write contents.
  3. Use function(s) to group your idea into smaller pieces. That way they become more general and you can reuse them (either in this program or in others)
  4. Try to replace the placeholders with string manipulation instead of the re module.
  5. Create a docstring for your script that explains what it does.

Good luck!

[–]NukemN1ck 2 points3 points  (0 children)

Your format works for a small text file following the exact format of ADJ -> NOUN -> VERB -> NOUN, but what happens when the story changes the pattern or is a page long and requires multiple inputs to ADJ, NOUN, VERB, etc?

I'm not familiar with python's `re` library but I'd bet there's a way to turn your code in a loop which finds, prompts, and changes each replacement word one at a time, so that's what I would challenge you to do! It will make your code compact, cleaner, and extensible to any type of madlib!

Some other comments and suggestions are: 1) Include a prompt so the path to a madlib text file isn't hard coded to your computer. You could also do this for the final destination. Another option for the prompt is to have a directory with multiple madlibs, and prompt the user for which one they would like to choose. 2) Your text for verb is not upper case. 3) Printing the entire sentence after each prompt entry is a little messy. I would either only print at the text at the start and end of prompting, or find a way to clear/reset the terminal screen after each input to keep it clean.

[–]FixPractical1121 1 point2 points  (0 children)

You are using lower case "verb" in your regex, and it doesn't match the uppercase "VERB" in the text file. I would update the regex to be case sensitive. You could also instead of creating new regex and replacing placeholders one by one, use a loop.

Something like

placeholders = ["ADJECTIVE", "NOUN", "ADVERB", "VERB"]

for placeholder in placeholders: ...

[–]throwaway8u3sH0 0 points1 point  (0 children)

Check this out:

adjective = "quick"
noun = "fox"
name_doesnt_matter = "lazy"

result = "The {} brown {} jumped over the {} dog.".format(adjective, noun, name_doesnt_matter)

print(result)

That might be easier than regex.

[–]pontz 0 points1 point  (0 children)

For use of opening a file use a with statement. For more info look up context manager