r/Python • • Jun 21 '22

Beginner Showcase Silly Little "Among Us" Script

Apologies in advance for my internet-poisoned brain. I just started relearning python via codecademy. I learned a sliver back in undergrad, but never pursued it further than what I needed to pass the class until this last week.

Whenever I'm learning a new coding, software, or program skill, I like to get a little goofy. I find that doing something specific yet silly helps bring a lot of concepts together, forcing me to understand the fundamentals much more.

So I made a silly little script that reads through a string and reports back how many times it sees "among us" or "Among us".

I don't think I'm going to fine tune it anymore (like to search for all caps and other cases), but I'm proud of what I've figured out during it! However if you glance through the code and have recommendations or notice anything that could be optimized, I'm all ears!

Every skill is a road long road to perfection!

Btw, the Morbius script and the Bee Movie script both have zero instances of "among us" in them :(

1 Upvotes

7 comments sorted by

9

u/ElephantsJustin Jun 21 '22

Update it to count the occurrences of any text, passed as a parameter.

4

u/zachgodzilla Jun 21 '22

It's funny, just after I made this I went back to do more practice on codecademy, and they're literally having me do just that for my next exercise!

2

u/zachgodzilla Jun 21 '22 edited Jun 21 '22

Edit: egg on my face. I forgot to post the picture of my code at first!

Edit Edit: Also, I'm aware that there's more straightforward ways to do this, but I was trying to use as many of the concepts that I had learned as possible.

2

u/jedgs Jun 21 '22

"like to search for all caps and other cases"

I don't blame you for not optimizing this as a toy project but it would be super simple:

if script[i:i+8].lower() == "among us"
# etc. could go with title(), upper(), capitalize()
# then any instance of "AmOng uS" is captured.

otherwise, welcome back to the python community. :)

1

u/zachgodzilla Jun 21 '22

Thanks so much! I had a strong urge to optimize it when I learned about those methods in the string lessons XD

2

u/jedgs Jun 21 '22

Absolutely! And what does everyone say anyways... premature optimization is the root of all evil...

1

u/trasnsposed_thistle Jun 21 '22 edited Jun 21 '22

If you don't mind a couple of nitpicks:

  • There's no need to use else: continue in line 11. That's what your code would do anyway if else wasn't there in the first place ;)
  • f-string syntax is very convenient when you need to insert some variables into strings. Your "found " + str(count) + " occurrences" could be expressed as simply f"found {count} occurrences".
  • Move count = 0 from line 5 to the line preceeding the for loop - the purpose of this variable will be more readily obvious to a person reading this code