Menu for a turn based game

Viewed 195

Our teacher has assigned us an assignment for doing a turned based game.

This only included name.strip() but this does not prompt player to input unique name:

def start():
   
   print("\nNew game started...Setting up Player 1's team...\n")
  
   for total_characters in range (1,4):
       name = input('Enter a unique name for unit #'+str(total_characters)+'==> ')
       if not name.strip():
           print('Cant be blank name,please provide a unique name')
           return start()
       else:
           role_selection()

def role_selection():
   for total_characters in range (1):
       role = input('Select unit #'+str(total_characters)+' type: (W)arrior, (T)anker, or Wi(Z)ard ==> ')                        
       total_characters+=1
       if role.upper() == 'W':
           pass             
       elif role.upper() == 'T':
           pass       
       elif role.upper() == 'Z':
           pass
       else:
           print("I don't understand what are you typing.")
           return role_selection()
3 Answers

At first glance, this stood out to me:

    if not name.strip():
        print('Unit name could not be blank.')
    invalid = True

Remember in python the indentation matters. You are setting invalid to True regardless of the if condition. Further down you have a while loop that checks it.

The other if conditions have invalid=True inside the condition. Plus you don't have invalid=False anywhere as far as I see, so you'll get an error if you don't declare it somewhere so it's always on the path before the while.

this doesn't seem like a specific problem, more an ask for general guidance for going about this kind of problem?

One thing to note is that your above script only uses functions (which store behaviour) whilst really for something like the turn based game, you need to store behaviour (attacks etc) and information (how much health is left etc).

I won't write the script for you, but here's an example of how you might define an rpg like entity, capable of attacking, being attacked by another entity etc:

class Entity:
"""Abstract class for any combat creature"""

def __init__(self, name, health):
    self.name = name
    self.health = health
    self.is_alive = True

def attack(self, other):
    dmg = random.randint(7, 12)
    other.be_attacked(dmg)

def be_attacked(self, dmg):
    self.health = self.health - dmg
    if self.health <= 0:
        self.die()

def die(self):
    self.is_alive = False

def check_life(self):
    return self.is_alive

You can then initialise a bunch of these to make up the 'team' you where talking about:

one_person = Entity('Lee', 34)
another_person = Entity('Someone Else', 21)

etc. Hope that helps a bit. Good luck with your project and have fun!

There are things that doesn't make sense :

  • You have the exact same function twice :

    def set_up(team_size)

    def set_up(name)

  • You are doing :

    for total_units in range(team_size):
        [...]

        invalid = True

        [...]

        while invalid: # Infinite loop
            set_up() # What's this function ?

As you can see from the comments in the code above, you never set invalid to False, leading to the infinite loop.

Note: My recommendation is that you should check out some tutorial on python before moving on coding a complex project, because your homework is not that easy.

Edit :

From the new code you've posted, you could do something like this :

def new_game():
  names = []
  for unit_id in range(1,4):
    print(f'Enter a unique name for unit #{unit_id}  ->')
    empty = True                                            
    while empty:
      name = input('>')                                                              
      if name == "":
        continue
      empty = False
      if name in names:
        print('Unit name must be unique.')                                                                              
      else:
        print('Name looks good!')
        names.append(name)

  python menu
Related