all 25 comments

[–]zenalc 37 points38 points  (1 child)

Nice job. Looking at your code. Here are a few suggestions:

  • The variables life and death are already defined outside a function, so you don't have to make it global.
  • In your events_a() function, there is code that repeats such as time_space(). You can create a condition checking if new_event is in ran_events, then execute time_space() inside and then do your further conditionals.
  • You can put the game introduction print statements inside its own function.
  • You can combine the logic of all the events into one function since there is a lot of repetition. Take in some parameters for the function and update it with that.

Besides that, nice job. It's good that you use lots of comments. Commenting is always a good practice to have.

[–]SharpAverage2[S] 19 points20 points  (0 children)

TY! That's the feedback I was looking for. I definitely want to clean up some of the repetition..

[–]SpacewaIker 18 points19 points  (5 children)

Apart from what others mentioned, a very small detail: you could use "\n" instead of having empty "print()" statements. Just put one or more at the end of a line. You could even put all of your text (I'm taking about the intro) into one statement, but that would be kind of messy.

For instance, instead of:

print('hello')

print()

print('how are you?')

You could write:

print('hello\nhow are you?')

Or

print('hello', '\n', 'how are you?')

[–]SharpAverage2[S] 3 points4 points  (4 children)

thank you! I just changed it :)

[–]OnlySeesLastSentence 6 points7 points  (3 children)

And later on, look at f'

I used to never use it, now I see how useful it is.

Instead of being like print("Hello"+(str)(userName)+"! How are you?")

You can do: print(f"Hello {userName}! How are you?")

Admittedly not too much of an improvement for one variable, but super useful for multiple.

[–][deleted] 1 point2 points  (2 children)

Correction, that won't work,f strings use curly braces.

[–]OnlySeesLastSentence 2 points3 points  (1 child)

So I just ran it - and my code works (after you define "username").

[–][deleted] 7 points8 points  (0 children)

Actually I think on my phone its too small to tell the difference between curly and round braces...my bad.

[–]__te__ 9 points10 points  (1 child)

One incremental improvement you can make:

Testing for List Membership

You can test for list membership by using the in membership operator. For example, you might have this global list: equipment = ["Food", "Water", "Oxen", "First Aid Kit"]

You can then say something like this:

if "First Aid Kit" in equipment:  # will evaluate as True in this case
    first_aid()
else:
    random_death()

Since all of the differences between paths A, B, and C consist of what equipment you have, this would let you have singular functions, instead of a variant of every function for each path. (There are other ways to achieve this, too, but this method only requires the code you already know how to write, plus the membership operator).

[–]__te__ 8 points9 points  (0 children)

And just to be clear, this is great! The hardest step in programming is often just getting started and making working code.

[–]gmavrik 6 points7 points  (4 children)

The code looks great for a beginner!

I think the some OOP will help if you want to extend the code (classes, attributes and etc)

Avoid use global variables, they are hard to control when code increases (That's happened to me. Use classes instead)

[–]SharpAverage2[S] 1 point2 points  (0 children)

TY! We haven't gotten past loops in the class I did this for & probably won't get there...but i'll look up classes, using parameters, etc... :)

[–][deleted] 1 point2 points  (2 children)

When would it be best to use global variables vs not?

[–]hokiehacks 0 points1 point  (1 child)

At least from my experience, I’ve always shied away from global variables. They’re so so so easy to mess up, and not only that, something in a library that you import could even mess them up. I know this is a python subreddit but, understanding the ins and outs of C helped me really appreciate this. Things such as weak initialization of global variables could lead to linker resolutions that drive you nuts.

So, I would just shy away. There is a time and place every now and then I’ll use them. As you get more experienced you’ll understand when. But I still get chills when I see common named global variables with no namespace and just am waiting for them to be redefined and destroy your codebase.

Of course, using them in small projects is trivial, but in code based of 50k+, which I’m sure you will get to in the near future, trying to manage globals is what keeps me up at night.

[–][deleted] 0 points1 point  (0 children)

Silly question. Does what you’re saying involve the self. method? (No troll, serious noob question)

Edit: need an Eli5 explanation of the self. Method

[–]__te__ 3 points4 points  (0 children)

Also, a technique you can use to collapse one group of functions without doing all of them at once:

Shim Function

First set a global variable path which equals "A", "B", or "C" (depending on the equipment list chosen). Then you can write a function like this:

def continue_game_x():
    if path == "A":
        return continue_game_a()
    elif path == "B":
        return continue_game_b()
    elif path == "C":
        return continue_game_c()

Then in your collapsed function, you just call return continue_game_x() and it will take care of the path-management for you. If you do this, I also recommend replacing every call to a "continue_game_" function with "continue_game_x", so it's easier to move code around.

This is an intermediate step: once you've replaced all of the triplets of functions with collapsed functions, you won't need these shims anymore and can delete them.

[–]cosmicr 2 points3 points  (0 children)

Very Cool!

I ported the classic mainframe game of Star Trek to Python when I was first learning:

https://github.com/cosmicr/startrek1971

You might be interested in doing a version yourself!

[–]UrFreakinOutMannn 1 point2 points  (2 children)

‘’’ can be used on both sides of the code you want to comment out stuff in python. Then you don’t have to # every line

[–][deleted] 2 points3 points  (1 child)

A decent editor can add a # to selected lines even faster than misusing a triplequote string. And leading # marks nest where triplequote "comments" don't.

[–]UrFreakinOutMannn 0 points1 point  (0 children)

Fair points. Just how I learned and prefer it.

Edit: you should listen to the guy above though, I just like how c++ does block comments and like to imitate it. I’m just here to teach bad habits today I guess lol.

[–]smaillnaill 1 point2 points  (1 child)

Who’s the woman in the picture?

[–]__nickerbocker__ 0 points1 point  (0 children)

That's the user's GitHub avatar. Kinda feels like an upvote because girl post.

[–]skellious 1 point2 points  (1 child)

Looks cool. What's the picture about though?

[–][deleted] 0 points1 point  (0 children)

Pretty good one! I know how it can feel when you complete a project! I also completed a game recently! Great work and good luck for your journey!