0

I only recently started taking classes in CIS and thus am unfamiliar with python to say the least. I was hoping I could get a few suggestions on a program I recently started. I've written a 5th edition D&D initiative tracker, the trouble I'm having is how to break it into modules from here. I am just now learning list(arrays) in python and am at a loss particularly arguments and parameters in regards to dealing with lists. Thanks in advance!

Code to follow

from random import randint

# Entering player names at the beginning of the session
def main():
    print("Enter PC names, separated by commas (David,Matt,Peter,etc.):")
    pcs = input().split(',')

# Loops every time a new battle has started
    while True:

# Setting up enemies
        num_foes = int(input("How many foes? "))
        foes = []
        foes = ["Enemy " + str(n+1) for n in range(num_foes)]

# Setting up enemy initiative modifiers
        foe_init_mods = []
        for foe in foes:
            foe_init_mods.append(int(input(foe + "'s initiative modifier? ")))

    # Setting up player initiative modifiers
            pcs_init_mods = []
            for pc in pcs:
                pcs_init_mods.append(int(input(pc + "'s initiative modifier? ")))
    
    # Setting up initiatives
            chars = []
            for i, pc in enumerate(pcs):
                chars.append([randint(1, 20) + pcs_init_mods[i], pc])
            for i, foe in enumerate(foes):
                chars.append([randint(1, 20) + foe_init_mods[i], foe])
    
    # Ordering characters by initiative
            chars.sort()
    
    # Displaying characters in order of initiative
            print("\n--- Initiative List ---")
            for i, char in enumerate(chars[::-1]):
                print(str(i+1) + ". " + char[1] + " (" + str(char[0]) + ")")
    
    # User decides if loop should terminate or not
            break_loop = input("\nAnother battle? (Y/N) ") == "N"
            if break_loop:
                print("Bye!")
                break
            else:
                print("Onwards!")
    
    
    main() 
Community
  • 1
  • 1
S.Ahl
  • 1
  • Is there a *specific* problem with this code? – Norrius Mar 11 '18 at 13:00
  • Sorry I posted this at the end of a long day of homework. I'm writing this for a class assignment. The problem is the professor wants us to write modular code. I understand that passing an array as an parameter requires both the name and length of the array. I don't understand how to do that when 1 session you might have 3 players and on a separate night you might have 8. How do I compensate for the change in array length – S.Ahl Mar 11 '18 at 17:33
  • You don't need the length of your list and you don't seem to use it anywhere in your code anyway. In a case you do need it, `len(my_list)` will obtain it in O(1) time. Can I ask if you use the word module in [the sense it's used in Python](https://docs.python.org/3/tutorial/modules.html)? – Norrius Mar 11 '18 at 17:38
  • I guess the correct term might be functions. For example the setting up enemies line might be 6 elements long in its first time through, and 10 elements in the second run through. – S.Ahl Mar 11 '18 at 17:48
  • You could make a [function](https://stackoverflow.com/questions/32409802/basic-explanation-of-python-functions) that takes a number and initialises that many enemies. Again, I don't see why you are expecting difficulties: your code already handles the variable number of PCs and NPCs. – Norrius Mar 11 '18 at 18:50
  • I appreciate the help! I was operating under the assumption that I had to determine the array length in order to pass it as a parameter when breaking this block of code into smaller functions. As was stated in the course text book. The error was that I forgot to account for the fact that its written agnostic, not for python specific.long story short, it was late, i anticipated a much more challenging task based on reading material, im an idiot =) Thanks for the patients @Norrius – S.Ahl Mar 12 '18 at 22:58

0 Answers0