Needing some random.choices population of a dictionary help

What would be a good way of modifying this code to add in one ‘*’ character per hand? Would you use weights?? Or would you set x to the character I am wanting once per pass of the loop? Any insight on this would be appreciated.

VOWELS = 'aeiou*'
CONSONANTS = 'bcdfghjklmnpqrstvwxyz'
HAND_SIZE = 7

SCRABBLE_LETTER_VALUES = {
    'a': 1, 'b': 3, 'c': 3, 'd': 2, 'e': 1, 'f': 4, 'g': 2, 'h': 4, 'i': 1, 'j': 8, 'k': 5, 'l': 1, 'm': 3, 'n': 1, 'o': 1, 'p': 3, 'q': 10, 'r': 1, 's': 1, 't': 1, 'u': 1, 'v': 4, 'w': 4, 'x': 8, 'y': 4, 'z': 10
}




  
  hand={}
    num_vowels = int(math.ceil(n / 3))

    for i in range(num_vowels):
        x = random.choice(VOWELS)
        hand[x] = hand.get(x, 0) + 1
    
    for i in range(num_vowels, n):    
        x = random.choice(CONSONANTS)
        hand[x] = hand.get(x, 0) + 1
    
    return hand

More information is necessary. Do you want a fixed number of vowels and consonants and add in one asterisk? Or do you want the asterisk to replace one of your calculated numbers of vowels and consonants?

The first is simple. Do the pick you’re doing and just stick an asterisk on the end.

N = 7. Yes, needing 3 vowels normally. Now that I’m adding a wildcard function it will be 2 vowels and an asterisk.

I guess maybe I was just overcomplicating it. I just feel like this isn’t the best code but works for my use currently. It gives 2 vowels 1 asterisk.

    hand={}
    num_vowels = int(math.ceil(n / 3))

    for i in range(num_vowels):
        x = '*'
        if x not in hand:
            hand[x] = hand.get(x, 0) + 1
        else:
            x = random.choice(VOWELS)
            hand[x] = hand.get(x, 0) + 1

Thank You!

Since you only want one asterisk, I wouldn’t put it in the loop at all.

 hand={}
    num_vowels = int(math.ceil(n / 3))

    hand["*"] = 1
    for i in range(num_vowels - 1):
        x = random.choice(VOWELS)
        hand[x] = hand.get(x, 0) + 1

Okay, thanks! I really appreciate it. That looks better for what I was trying to accomplish.

Just to the stuff below:

By Rufus Polk via Discussions on Python.org at 18Apr2022 13:49:

   x = '*'
   if x not in hand:
       hand[x] = hand.get(x, 0) + 1

If x is not present, hand.get(x,0) will always be zero, so this
could be:

hand[x] = 1
   else:
       x = random.choice(VOWELS)
       hand[x] = hand.get(x, 0) + 1

If x is in hand then you don’t need hand.get, just go:

hand[x] = hand[x] + 1

or more clearly:

hand[x] += 1

Cheers,
Cameron Simpson cs@cskk.id.au