harry pop
harry pop

Reputation: 29

can i call a function like this? by creating an array of functions/? NEW TO CODING

The code was designed in a project to make hangman The user needs to guess letters, Give the user no more than 6 attempts for guessing wrong letter. This will mean you will have to have a counter. You can download a ‘sowpods’ dictionary file or csv file to use as a way to get a random word to use.

import random

alpha = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']
beta = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']
func = [hang1(), hang2(), hang3(), hang4(), hang5(), hang6(), hang7(), hang8(), hang9(), hang10(), hang11(), hang12()]

def randWord():
    words = []
    with open('sowpods.txt', 'r') as f:
        line = f.readline().strip()
        words.append(line)
        while line:
            line = f.readline().strip()
            words.append(line)
    random_index = random.randint(0 , len(words))
    rWord = words[random_index]
    lowrWord = rWord.lower()
    return lowrWord

def userIn():
    value = input("Guess a letter?\n")
    if value in alpha:
        return value 
    else:
        print("Not in alphabet! Try again")
        userIn()
    return value

def matchWord():
    while beta != []:
        randoW = randWord()
        myChoice = userIn()
        i = 0
        while i == 0:
            def printA():
                if myChoice in beta:
                    beta.remove(myChoice)
                print(*beta, sep = ", ")

            if myChoice in randoW:
                print("You are correct!")
                printA()

            else:
                print("Your are incorrect!")
                printA()
                hang1()
                i = i + 1
        else:
            def printA():
                if myChoice in beta:
                    beta.remove(myChoice)
                print(*beta, sep = ", ")

            if myChoice in randoW:
                print("You are correct!")
                printA()
                func[i]

            else:
                print("Your are incorrect!")
                printA()
                hang1()
                i = i + 1
                func[i]

matchWord()

def hang1():
    print("|       ")
    print("|       ")
    print("|       ")
    print("|       ")
    print("|         ")
    print("|         ")
    print("|         ")
    print("|")
    print("|")
    print("|")
    print("| ")
    print("|  ")
    print("|")

def hang2():
    print("|       ")
    print("|       ")
    print("|       ")
    print("|       ")
    print("|         ")
    print("|         ")
    print("|         ")
    print("|")
    print("|")
    print("|")
    print("| ")
    print("|  ")
    print("|____________________________")

def hang3():
    print("|       ")
    print("|       ")
    print("|       ")
    print("|       ")
    print("|         ")
    print("|         ")
    print("|         ")
    print("|")
    print("|")
    print("|\\")
    print("| \\")
    print("|  \\")
    print("|___\\_______________________")

def hang4():
    print(" _______________")
    print("|       ")
    print("|       ")
    print("|       ")
    print("|       ")
    print("|         ")
    print("|         ")
    print("|         ")
    print("|")
    print("|")
    print("|\\")
    print("| \\")
    print("|  \\")
    print("|___\\_______________________")

def hang5():
    print(" _______________")
    print("|   /     ")
    print("|  /      ")
    print("| /       ")
    print("|/        ")
    print("|         ")
    print("|         ")
    print("|         ")
    print("|")
    print("|")
    print("|\\")
    print("| \\")
    print("|  \\")
    print("|___\\_______________________")

def hang6():
    print(" _______________")
    print("|   /     |")
    print("|  /      |")
    print("| /       |")
    print("|/      ")
    print("|         ")
    print("|          ")
    print("|           ")
    print("|")
    print("|")
    print("|\\")
    print("| \\")
    print("|  \\")
    print("|___\\_______________________")

def hang7():
    print(" _______________")
    print("|   /     |")
    print("|  /      |")
    print("| /      _|_")
    print("|/      |___|")
    print("|         ")
    print("|          ")
    print("|           ")
    print("|")
    print("|")
    print("|\\")
    print("| \\")
    print("|  \\")
    print("|___\\_______________________")

def hang8():
    print(" _______________")
    print("|   /     |")
    print("|  /      |")
    print("| /      _|_")
    print("|/      |___|")
    print("|        | | ")
    print("|          ")
    print("|           ")
    print("|")
    print("|")
    print("|\\")
    print("| \\")
    print("|  \\")
    print("|___\\_______________________")

def hang9():
    print(" _______________")
    print("|   /     |")
    print("|  /      |")
    print("| /      _|_")
    print("|/      |___|")
    print("|        | | ")
    print("|       /   ")
    print("|      /     ")
    print("|")
    print("|")
    print("|\\")
    print("| \\")
    print("|  \\")
    print("|___\\_______________________")

def hang10():
    print(" _______________")
    print("|   /     |")
    print("|  /      |")
    print("| /      _|_")
    print("|/      |___|")
    print("|        | | ")
    print("|       /   \\")
    print("|      /     \\")
    print("|")
    print("|")
    print("|\\")
    print("| \\")
    print("|  \\")
    print("|___\\_______________________")

def hang11():
    print(" _______________")
    print("|   /     |")
    print("|  /      |")
    print("| /      _|_")
    print("|/      |___|")
    print("|      __| |")
    print("|     |__   ")
    print("|       /   \\")
    print("|      /     \\")
    print("|")
    print("|")
    print("|\\")
    print("| \\")
    print("|  \\")
    print("|____________________________")

def hang12():
    print(" _______________")
    print("|   /     |")
    print("|  /      |")
    print("| /      _|_")
    print("|/      |___|")
    print("|      __| |__ ")
    print("|     |__   __|")
    print("|       /   \\")
    print("|      /     \\")
    print("|")
    print("|")
    print("|\\")
    print("| \\")
    print("|  \\")
    print("|___\\_______________________")

hey! thanks everyone for the help! basically, im not sure if im allowed to call functions in the array format like I did- any recommendations? Im pretty new so anything will help!

Upvotes: 1

Views: 32

Answers (1)

Barmar
Barmar

Reputation: 782106

The functions are being called when you create the list. When you write something like

funcs = [hang1(), hang2(), ...]

it's similar to

temp1 = hang1()
temp2 = hang2()
...
funcs = [temp1, temp2, ...]

If you want to call the functions later, just put the function names in the list:

funcs = [hang1, hang2, ...]

Then add parentheses when you access the list element:

funcs[i]()

Also, Python is interpreted sequentially. You need to put all the hangX definitions before you refer to them in funcs.

Upvotes: 2

Related Questions