4
\$\begingroup\$

This is my first Python program and all it does is print out a description of the specific "Alien Class" that the user inputs.

Looking for tips and pointers and what I can do better and what I am doing well.

# Initializing the alien army with different alien classes

alien_army = {
    'rogue': {
        'name': 'rogue',
        'color': 'green',
        'size': 'small',
        'speed': 'fast',
        'damage': 'small'
    },
    'warrior': {
        'name': 'warrior',
        'color': 'blue',
        'size': 'medium',
        'speed': 'average',
        'damage': 'heavy'
    },
    'hunter': {
        'name': 'hunter',
        'color': 'dark green',
        'size': 'average',
        'speed': 'average',
        'damage': 'average'
    }
}

# Adding an Alien class 'Monk' to the alien_army

alien_army['monk'] = {
    'name': 'monk',
    'color': 'light gray',
    'size': 'small',
    'speed': 'quick',
    'damage': 'average'
}

class_names = []
for k, v in alien_army.items():
    class_names.append(k)

# Function that shows the alien that the user typed

def showPickedAlien(a):
    print(f"{a['name'].upper()}'S are {a['color'].title()} in color, {a['size'].lower()} in size, {a['speed'].lower()} in speed and they do a {a['damage'].lower()} amount in damage.")

# While loop flag to run or stop loop

input_enabled = True

# While loop to keep displaying the alien class that the user inputs

while input_enabled:
    user_input = input("Which class do you want to see? ").lower()
    input_alien = alien_army.get(user_input)

    # Checking if user entered a valid alien class name

    if user_input in class_names:
        showPickedAlien(input_alien)
    else:
        print(f"{user_input.upper()} is not a valid alien class name.")

    # Asking user if they want to see another alien class or not

    input_for_quit = input("Do you want to see another class? ").lower()

    # Determining if the user wants the loop to stop or not

    if input_for_quit == 'no' or input_for_quit == 'n':
          print("Goodbye.")
          input_enabled = False
    elif input_for_quit == 'yes' or input_for_quit == 'y':
          input_enabled = True
\$\endgroup\$
3
\$\begingroup\$

Good job on your first Python program!

You used a Dictionary to store all the alien classes. There are many other alternatives to do the same task. The most common one for this kind of problem is Python Classes (https://www.w3schools.com/python/python_classes.asp).

You could simply create a class called myArmy and then create your soldiers as objects.

#Create a class called myArmy
class myArmy:

  #Use the __init__() function to assign values for name, color...
  def __init__(self, name, color, size, speed, damage):

    self.name = name
    self.color = color
    self.size = size
    self.speed = speed
    self.damage = damage

#Create your soldiers (Referred to as "Objects" here. The __init__() function is called automatically every time the class is being used to create a new object.
rogue = myArmy('rogue', 'green', 'small', 'fast', 'small')
warrior = myArmy('warrior', 'green', 'medium', 'average', 'heavy')
hunter = myArmy('hunter', 'dark green', 'average', 'average', 'average')

#Access Rogue information for example
print(rogue.name, rogue.color, rogue.size, rogue.speed, rogue.damage)

Next step should be to give them attack/defense values and attack speed, and let the user make them battle each other!

\$\endgroup\$
2
\$\begingroup\$
  • According to the PEP8 widely-accepted style, your function should be named show_picked_alien.

  • You don't need a class_names list, as you can just do if key in dictionary

  • If it always coincides with the key, you don't need the name attribute

  • You don't need a input_enabled variable: you can just break out of the loop, or better, put that in a function and return

  • You should avoid executable code at the top level of your module, for future extensibility (it also always helps having smaller functions). Use a classic: if __name__ == "__main__": main()

  • It's better if you pick one quote type (single or doubles) and stick with it, within a program (also from PEP8). But don't pick one style "for life" and apply it on projects using other styles.

  • Maybe that's pushing it too far, but if your goal is to make a real application, you can transform your attributes, at least speed/size/damage into enums rather than strings (or use string constants). Because later on you will have checks like if speed == 'average' and don't want to repeat string literals and have to make them coincide in several places

  • I second MarkH's suggestion of making a class (but with a PEP8-compliant name). If you do this, implement __str__ on it (maybe the same as your current showPickedAlien if not too verbose). That will help with your debugging later on.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.