Click here to Skip to main content
15,867,308 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Essentailly, my program is supposed generate a list, then have the user input a character they want to replace and a character they want to replace it with. This issue is that my functions for these processes just straight up aren't working.

import random

MAX_ELEMENTS = 25





MIN_CHAR = 33
MAX_CHAR = 126


# args(none)
# return(a randomly generated character) from ! to ~
def generateElement():
    # Declare variables for function
    aChar = " "
    dieRoll = -1

    dieRoll = random.randrange(MIN_CHAR, (MAX_CHAR + 1))
    aChar = chr(dieRoll)  # Function to convert ASCII code to a character
    return aChar


# args(none)
# Creates a list of 1 - 100 elements each element a string of length 1
# return (reference to the list)
def createList():
    # Declare variables for function
    global aList
    aList = []
    size = -1
    i = -1

    size = random.randrange(1, (MAX_ELEMENTS + 1))
    i = 0
    while i < size:
        aList.append(generateElement())
        i = i + 1
    return aList, size


# args(none)
# displays an index followed by each list element on it's own line
# return(nothing)
def display(aList, size):
    # Declare variables for function
    i = -1

    i = 0
    while i < size:
        print("Element at index[%d]=%s" % (i, aList[i]))
        i = i + 1

    # Starting execution point for the program

class ReplacedCharacter:
    findChar = "&"

class ReplacingCharacter:
    replaceChar = "."

def getFindAndReplaceCharacters():
    ReplacedCharacter.findChar = input("Enter the character you want to find here (ONE character only):")
    ReplacingCharacter.replaceChar = input("Enter the character you want to replace here (ONE character only):")





def replace_values():
    for list in aList:
        new_list = aList.replace(ReplacedCharacter.findChar, ReplacingCharacter.replaceChar)
    aList.append(new_string)




def start():
    # Declare variables local to start
    aList = []
    size = -1

    aList, size = createList()
    display(aList, size)
    getFindAndReplaceCharacters()
    replace_values()
    display(aList, size)



start()


What I have tried:

I've tried to move around the parameters and even added another function to try and troubleshoot, but I've gotten no luck so far. All I keep getting is "aList isn't defined." What's the issue with my code??
Posted
Updated 18-Nov-22 22:31pm
v2
Comments
Richard MacCutchan 18-Nov-22 12:16pm    
What are all those backslash characters doing in your code?
Fikayo Akande 18-Nov-22 18:06pm    
I don't know they just kinda showed up as I was formatting the code.

1 solution

You have not explained where the problem occurs, so I am making some assumptions here
Python
def createList():
    # Declare variables for function
    global aList
    aList = []

You are declaring aList to be a global variable, but it does not exist in the global namespace, so it cannot be 'seen' by other functions. And your code is inconsistent as some functions use it as an input and/or return value.

You have one of two options:
1. Define aList in the global namespace by modifying your code thus:
Python
MAX_CHAR = 126
aList = []

But that means you need the global declaration in every one of your functions.

2. Use aList as an input parameter to every function, and a return value to any that modify it.

Note also that createList does not need to return the size of the list, as you can find that at any point by using the len() built-in function.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900