I'm trying to shore up my coding skills so I thought it would be fun to make a character stat creator in DnD. I created a main() function that creates a dictionary of possible stats (STR, DEX, etc.), sets the initial value as 0, then passes the dictionary to different functions based on a user input (3d6, 4d6). At the moment, the program just runs through the dictionary and assigns values as they're rolled.
This is the code so far
#!/usr/bin/env python3 # c_stats2.py import random import math # 4d6 drop lowest def m4d6(x): for stat in x: rolls = [] for roll in range(4): r = random.randint(1,6) rolls.append(r) # Drop lowest number del rolls[rolls.index(min(rolls))] x[stat] += sum(rolls) return x # 3d6 drop lowest def m3d6(x): for stat in x: rolls = [] for roll in range(3): r = random.randint(1,6) rolls.append(r) x[stat] += sum(rolls) return x def main(): method = input("Select stat method: ") stats = {'STR': 0, 'DEX': 0, 'CON': 0, 'INT': 0, 'WIS': 0, 'CHA': 0 } while True: if method == '4d6' or method == '4D6': stats = m4d6(stats) break elif method == '3d6' or method == '3D6': stats = m3d6(stats) break else: method = input('Please enter a valid method: ') continue for stat in stats: print(stat, ':' ,stats[stat], '\tBonus:' , math.floor((int(stats[stat]) - 10)/2)) print("Initiative:", math.floor((int(stats['DEX']) - 10)/2)) main() I wanted to add an option that would allow the user to actually choose where the stats were going. My thinking was that I would create a list of values rolled, assign them to a stat in the dictionary, then remove that value from the list of rolled values and return the dictionary to the new values. Here's the code so far.
# 4d6 drop lowest, choose stats def m4d6c(x): val = [] for dice in range(6): rolls = [] for roll in range(4): r = random.randint(1,6) rolls.append(r) del rolls[rolls.index(min(rolls))] dice += sum(rolls) val.append(dice) for stat in x: print(val) print(str("Insert value for {}.").format(stat)) dic = input('Value: ') if dic in val: x[stat] += dic val.remove(dic) continue else: print("Please enter a valid value") continue return x The problem is the values aren't being assigned to the stats and aren't being removed from val list. Can anyone tell me what I might be doing wrong here?
1 Answer
Looks like you were close, but you forgot that input returns a string, and in m4d6c you need an int for your logic to work.
Current:
dic = input('Value: ') Needed:
dic = int(input('Value: ')) 1