Python Forum
A simple script - looking for feedback
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
A simple script - looking for feedback
#1
Hi all

I have made a simple script for learning purposes more than for any practical purposes. Since I'm still new to Python I'd like some feedback.

I would guess there is a function somewhere to look for streaks of the same value in lists, but I wanted to avoid that and make something myself. But if you spot something that's either way too long for what it does or is not very 'pythonian' I'd like to hear about it and see som suggestions.

Basically my script generates 100 0/1's (coinflips you could say) and then looks at how how the streaks are of either value. Other than for learning python it's quite interesting that the streaks are much more common and longer than what people guess when asked.

import random

#Description
#A simple script to look at randomness and variance(binomial)
#1 Generate x amount of numbers(here 100) randomly and store in 'dblist'
#2 prepare lists
#3 second for loop fills streaks0/streaks1 with length of streaks
#4 maxstreaks0/1 lists store max streak values for each 100 numbers
#5 main function is run repeatedly - currently 100 times(10k numbers total)

#

dblist = [] #stores the randomly generated 1/0
shorttermlist0 = 0 #first storage of the streaks length
shorttermlist1 = 0
streaks0 = [] #used in the for loop
streaks1 = [] #used in the for loop
maxstreaks0 = [] #data for each of the samplings(100 numbers each)
maxstreaks1 = [] #data for each of the samplings(100 numbers each)

def random100():
    global dblist
    global shorttermlist0
    global shorttermlist1
    global streaks0
    global streaks1
    global maxstreaks0
    global maxstreaks1

    for i in range (0,100):
        currentnum = random.randint(0,1)
        #print(currentnum)
        dblist.append(currentnum)

    #print (dblist)

    for i in range (0,100):
        if dblist[i] == 1:
            shorttermlist1 += 1
            streaks0.append(shorttermlist0)
            shorttermlist0 = 0
        else:
            shorttermlist0 += 1
            streaks1.append(shorttermlist1)
            shorttermlist1 = 0

    maxstreaks0.append(max(streaks0))
    maxstreaks1.append(max(streaks1))
    streaks0 = []
    streaks1= []
    dblist = []


for _ in range(100):
    random100()


#print(list1)
#print(list0)
print('overallist0 - max values for each sample of 100:  ', sorted(maxstreaks0,reverse=True))
print('overallist1 - max values for each sample of 100:  ', sorted(maxstreaks1,reverse=True))


'''#known limitations
streak ongoing at 100th number isn't registered

'''
Reply
#2
I'll start by listing a few problems I see with the code:
  • Initializing all of those variables at the start of your code is not needed
  • The global statement is very rarely a good idea; pass arguments to functions and return values instead of modifying globals and appending to lists, and if you need state, use classes
  • You should never write loops over the indices of a list if you actually want elements. Instead of:
    for i in range (0,100):
        if dblist[i] == 1:
    use
    for number in dblist:
        if number == 1:
    You can take a look at this talk to learn more about python's loops.
  • A function should only do one thing. Your random100() function creates a list of numbers, finds streaks in it, and returns the longest ones. Splitting it into a few simpler functions would make the code more readable and easier to test and maintain.

All that said, what's really important is you wrote some code that does what you want.
That, along with reading others people's code, is the best way to improve.
Reply
#3
Thanks a lot for the feedback!

My changes since previous version of the script:

1) completely removed the global statements and the initializing of variables at the beginning.
2) changed from using indices of lists to using the elements directly(I guess that is it)
3) separated the script into 3 functions

Here is what it looks like now:

import random

#Description
#A simple script to look at randomness and variance(binomial)
#1 The function 'numbergenerator' generates x amount of numbers
# (here 100) randomly and stores them in 'dblist'
#2 'streakcounter' returns longest streak in each 100 number sample
#3 'maxcounter' creates two lists with streaks from each 100 number sample

def numbergenerator():
    storagelist = []
    for i in range(0,100):
        currentnum = random.randint(0,1)
        storagelist.append(currentnum)
    return(storagelist)

def streakcounter():
    dblist = numbergenerator()
    streaks0 = []  # used in the for loop
    streaks1 = []  # used in the for loop
    shorttermlist0 = 0
    shorttermlist1 = 0
    for i in dblist:
        if i == 1:
            shorttermlist1 += 1
            streaks0.append(shorttermlist0)
            shorttermlist0 = 0
        else:
            shorttermlist0 += 1
            streaks1.append(shorttermlist1)
            shorttermlist1 = 0
    return(max(streaks0),max(streaks1))

def maxcounter(times):
    streakcounterlist0 = []
    streakcounterlist1 = []
    for _ in range(times):
        zeros, ones = streakcounter()
        streakcounterlist0.append(zeros)
        streakcounterlist1.append(ones)
    print('max streaks of 0s from %i samples' %(times),streakcounterlist0)
    print('max streaks of 1s from %i samples' %(times), streakcounterlist1)

maxcounter(100)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Simple Python script, path not defined dubinaone 3 2,654 Nov-06-2021, 07:36 PM
Last Post: snippsat
  Need help creating a simple script Nonameface 12 4,425 Jul-14-2020, 02:10 PM
Last Post: BitPythoner
  Simple text to binary python script gmills13 2 2,760 Feb-04-2020, 08:44 PM
Last Post: snippsat
  Made a simple script for android (really simple) but it is not running anddontyoucomebacknomore 2 2,325 Mar-06-2019, 12:19 AM
Last Post: anddontyoucomebacknomore
  Feedback and help tomX 13 5,395 Dec-31-2018, 11:00 PM
Last Post: Larz60+
  Simple script that seems to misbehave? Nwb 1 2,295 Jun-10-2018, 05:30 AM
Last Post: Nwb
  First time with Python.. need help with simple script shakir_abdul_ahad 7 5,417 May-06-2018, 09:28 AM
Last Post: killerrex
  help with a simple script juanb007 4 3,567 May-01-2018, 08:06 PM
Last Post: ThiefOfTime
  Simple script writted by a dumb dude, myself mm14ag 2 2,695 Apr-28-2018, 11:48 AM
Last Post: mm14ag
  Need help with a simple AHK script Stabu 0 2,071 Feb-24-2018, 08:27 PM
Last Post: Stabu

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020