[Tutor] What's going on?

Ed Connell edwinconnell at gmail.com
Sun Apr 30 17:31:39 EDT 2023


Greetings,

I got interested in one of your earlier problems, but THAT PROBLEM is not
what I am asking about. It was to generate a random sequence, each taken
from a predefined pool. However, nothing is to be used more than four
times. Moderately interesting.

What I am asking about is this.  The first time it runs perfectly. However
subsequent runs, sooner or later, foul up.
###############################
import random
import time

for run in range(10): #usually 1 but trying multiple runs through in one
trip.

    # build result, number choices, and choice count lists
    res = []
    nums = []
    ct = []

    for i in range(10):
        ct.append(0)
        nums.append(i)

    print('\n\n', run, ' start\n')

    #time.sleep(0.016) # seems to work for 0.016 or more uncommented

    for _ in range( 30 ): # tried 10 - 40
        random.shuffle(nums)
        res.append(nums[0])
        print(res)
        ct[nums[0]] += 1
        if ct[nums[0]] == 4:
            print( 'Removing', nums[0],nums )
            nums = nums[1:]
            print('leaving', nums )

    print()
    print('numbers left',sorted(nums))
    print('result',res)
    print('number used of each',ct)
####################################
The problem seems to be a difference between printing and calculation speed
but maybe not.

I tried mostly using Geany, but also just ran the .py file.

The bad output has incomplete lines as well as missing sections.

I will appreciate your ideas.

Oh, I am running on Windows 11, an hp with AMD 7.

Ed Connll
-- 
I have a right and a left brain, but there is nothing right in the left one
and there is nothing left in the right one!


More information about the Tutor mailing list