# Please help me improve this code

**URL:** <https://forum.freecodecamp.org/t/please-help-me-improve-this-code/387998>\
**Category:** General\
**Created:** [May 13, 2020, 4:41am UTC](https://forum.freecodecamp.org/t/please-help-me-improve-this-code/387998 "2020-05-13T04:41:34Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![rperrett](https://avatars.discourse-cdn.com/v4/letter/r/839c29/32.png) [@rperrett](https://forum.freecodecamp.org/u/rperrett)\
**Post date:** [May 13, 2020, 4:41am UTC](https://forum.freecodecamp.org/t/please-help-me-improve-this-code/387998/1 "2020-05-13T04:41:34Z")

</div>

My code works. It reads in a textfile of letters (eg alphabet) and creates a new textfile of the same letters in random order.  
I have used functions and global variables.  
Please help me by teaching me how to pass the correct parameters into my functions so the code still works but without using global variables.

```auto
def readfile():
    print('Reading File')
    global listlines
    f = open('alphabetSorted.txt')
    for line in f:
        #print(line.strip())
        listlines.append(line)
    #print(listlines)
    f.close()

def makerandom():
    print('Randomising Data')
    global newlist
    newlist = listlines.copy()
    random.shuffle(newlist)
    #print('NEWLIST',newlist)
     

def writefile():
    print('Writing File')
    global newlist
    f = open("alphabetRandom.txt", "w")
    #print(newlist)
    f.writelines(newlist)
    f.close()

def main():
    readfile()
    makerandom()
    writefile()

#########################
import random
listlines=[]
newlist =[]
main()

```

---

<div class="post-metadata">

**Author:** ![ahmedkhaled](https://avatars.discourse-cdn.com/v4/letter/a/e274bd/32.png) [@ahmedkhaled](https://forum.freecodecamp.org/u/ahmedkhaled)\
**Post date:** [May 15, 2020, 8:31pm UTC](https://forum.freecodecamp.org/t/please-help-me-improve-this-code/387998/2 "2020-05-15T20:31:37Z")

</div>

If you don’t want to use global variables, then use parameters and return statements.  
Here’s my solution:

```auto
def readfile():
    print('Reading File')
    listlines = []
    f = open('alphabetSorted.txt')
    for line in f:
        listlines.append(line)
    f.close()
    return listlines

def makerandom(listlines):
    print('Randomising Data')
    
    # since you're not using this "newlist" elsewhere in the code, you can actually shuffle listlines directly
    newlist = []
    newlist = listlines.copy()
    random.shuffle(newlist)
    return newlist
     

def writefile(newlist):
    print('Writing File')
    f = open("alphabetRandom.txt", "w")
    f.writelines(newlist)
    f.close()

def main():
    listlines = readfile()
    listlines = makerandom(listlines)
    writefile(listlines)

#########################
import random
main()

```

I hope this helps, if you have any problem in understanding parameters or return statements, do not hesitate to tell me and I will explain further.

---

<div class="post-metadata">

**Author:** ![rperrett](https://avatars.discourse-cdn.com/v4/letter/r/839c29/32.png) [@rperrett](https://forum.freecodecamp.org/u/rperrett)\
**Post date:** [May 16, 2020, 2:08am UTC](https://forum.freecodecamp.org/t/please-help-me-improve-this-code/387998/3 "2020-05-16T02:08:35Z")

</div>

Thank you so much for your help. Forums are great. I always get help from forums.

You just taught me that it is possible to write a function that does not have arguments but returns a value. That confused me for a bit.

After watching a YouTube [https://www.youtube.com/watch?v=tUiE3KrvqU0&t=917s](https://www.youtube.com/watch?v=tUiE3KrvqU0&t=917s)  
I understood better. The video’s example is getting the time.

---

<div class="post-metadata">

**Author:** ![ahmedkhaled](https://avatars.discourse-cdn.com/v4/letter/a/e274bd/32.png) [@ahmedkhaled](https://forum.freecodecamp.org/u/ahmedkhaled)\
**Post date:** [May 16, 2020, 2:29am UTC](https://forum.freecodecamp.org/t/please-help-me-improve-this-code/387998/4 "2020-05-16T02:29:13Z")

</div>

Happy to help! Keep up the good work and happy coding 🙂

---

<div class="post-metadata">

**Author:** ![ILM](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/ilm/32/363239_2.png) [@ILM](https://forum.freecodecamp.org/u/ILM)\
**Post date:** [June 1, 2021, 6:53pm UTC](https://forum.freecodecamp.org/t/please-help-me-improve-this-code/387998/5 "2021-06-01T18:53:36Z")

</div>

Closed on June 1, 2021, 6:53pm UTC


