0

I'm very new to python and am trying to create a basic I Ching Hexagram creation program, and I'm running into issues in my first function.

import random    

def hexline(): 
#This flips three coins and produces a hexagram line 
    flip = random.randint(2,3)
    res = flip+flip+flip
    if res == 6:
        print('-- --x')
    elif res == 7:
        print('-----')
    elif res == 8:
        print('-- --')
    elif res == 9:
        print('-----x')

When hexline() is run, it only returns either -----x or -- --x (values of 9 or 6) indicating that randint(2,3) is only making one random selection for the whole function (rather than 3 distinct random choices) and adding them together with res. So res is producing only 2+2+2 or 3+3+3, rather than say 3+2+3 or 2+2+3. How might I generate a random selection multiple times in this function, rather than only once? Thanks.

samcorr
  • 3
  • 2

3 Answers3

2

When you say flip = random.randint(2,3), it calls the randint just once and sets the output value to flip. Therefore, adding together three flips just multiplies the value by three.

A very simple fix is to create three variables, flip1, flip2, flip3 each with a call to randint, then add these variables together to form res. Alternatively, add them all together at once.

As you progress with Python, you'll learn that you can produce similar effects with a for loop or list comprehension. You can use these to add things together an arbitrary number of times. Here's the for loop version:

total = 0
for i in range(1, 4):
    total = total + random.randint(2,3)
Gamma032
  • 441
  • 4
  • 7
1

This should work:

import random    

def hexline(): 
#This flips three coins and produces a hexagram line 
    res=0
    for x in range(0,3):
      res += random.randint(2,3)
    if res == 6:
        print('-- --x')
    elif res == 7:
        print('-----')
    elif res == 8:
        print('-- --')
    elif res == 9:
        print('-----x')
oppressionslayer
  • 6,942
  • 2
  • 7
  • 24
0

here, you're assigning a value to flip once, and you are not evaluating the randomness a second time. This means that you are effectively evaluating something more along the lines of this:

    a = 2
    res = a + a + a

Naturally, the above code should evaluate to 6, or 9 when the flip generates a 3.

To solve the problem you are experiencing, you need to evaluate the random.randint(2,3) three times, rather than the one you are doing right now, like so:

import random    

def hexline(): 
#This flips three coins and produces a hexagram line 

    res = random.randint(2,3) + random.randint(2,3) + random.randint(2,3)
    if res == 6:
        print('-- --x')
    elif res == 7:
        print('-----')
    elif res == 8:
        print('-- --')
    elif res == 9:
        print('-----x')

You could define a small function or a lambda to make this a little neater.

import random    

def hexline(): 
#This flips three coins and produces a hexagram line 
    flip = lambda: random.randint(2,3)
    res = flip()+flip()+flip()
    if res == 6:
        print('-- --x')
    elif res == 7:
        print('-----')
    elif res == 8:
        print('-- --')
    elif res == 9:
        print('-----x')

what happens here is nearly identical to the first solution, but it kind of renames the random.randint(2,3) to flip() by defining it as the lambda.

David Culbreth
  • 2,610
  • 16
  • 26