-2
import random
from words import words
import string

def get_valid_word(word):
    word = random.choice(words)
    while '-' in word or ' ' in word:
        word = random.choice(words)
    
    return word

def hangman():
    word = get_valid_word(words)
    word_letters = set(word)  # letters in word
    alphabet = set(string.ascii_uppercase)
    used_letters = set()
    
    user_input = ("type something: ")
    print(user_input)

I have been following along a YouTube python project, but when I use the import function the code doesn't seem to run. It executes nothing and says its done.

Gino Mempin
  • 25,369
  • 29
  • 96
  • 135
Jordan
  • 1
  • 3
    This code _defines_ the hangman function but never _calls_ it... – John Gordon Nov 05 '22 at 03:48
  • 1
    Does this answer your question? [Why is my Python function not being executed?](https://stackoverflow.com/questions/29652264/why-is-my-python-function-not-being-executed) – Gino Mempin Nov 05 '22 at 04:27

2 Answers2

1

Because you never called hangman():

If you are using a script try this at the end of script:

if __name__ = "__main__":
    hangman()

Otherwise, just call hangman() at the end.

Anurag Dhadse
  • 1,722
  • 1
  • 13
  • 26
-1

A function is a block of code that will only run when it has been called upon. You have declared two functions but never called them, therefore they cannot run. To fix this, simply call the functions like this:

hangman()
get_valid_word()

Add this to the very bottom of your code without an indent so that the compiler knows that is the main code. You should have something looking like this:

import random
from words import words
import string

def get_valid_word(word):
    word = random.choice(words)
    while '-' in word or ' ' in word:
        word = random.choice(words)
    
    return word

def hangman():
    word = get_valid_word(words)
    word_letters = set(word)  # letters in word
    alphabet = set(string.ascii_uppercase)
    used_letters = set()
    
    user_input = ("type something: ")
    print(user_input)

hangman()
get_valid_word()
TCK
  • 42
  • 6