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
#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


Messages In This Thread
RE: A simple script - looking for feedback - by glidecode - Feb-20-2018, 11:18 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Simple Python script, path not defined dubinaone 3 2,745 Nov-06-2021, 07:36 PM
Last Post: snippsat
  Need help creating a simple script Nonameface 12 4,695 Jul-14-2020, 02:10 PM
Last Post: BitPythoner
  Simple text to binary python script gmills13 2 2,853 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,403 Mar-06-2019, 12:19 AM
Last Post: anddontyoucomebacknomore
  Feedback and help tomX 13 5,621 Dec-31-2018, 11:00 PM
Last Post: Larz60+
  Simple script that seems to misbehave? Nwb 1 2,382 Jun-10-2018, 05:30 AM
Last Post: Nwb
  First time with Python.. need help with simple script shakir_abdul_ahad 7 5,582 May-06-2018, 09:28 AM
Last Post: killerrex
  help with a simple script juanb007 4 3,676 May-01-2018, 08:06 PM
Last Post: ThiefOfTime
  Simple script writted by a dumb dude, myself mm14ag 2 2,791 Apr-28-2018, 11:48 AM
Last Post: mm14ag
  Need help with a simple AHK script Stabu 0 2,114 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