0

I'm new to python and this is code for hangman. randomly taking a word and user is guessing the letters odf the word. This was one of problem from 12 beginner python project of freeCodeCamp.org video.In the video it shows no problem. but when I'm running this i get no error but no output as well.

 import random
 from words import words
 import string

 def get_valid_word(words):
     word = random.choice(words)  # randomly chooses something from the list
     while '_' in word or ' ' in word:
         word = random.choice(words)
     return word.upper()

 def main():
     word = get_valid_word(words)
     word_letters = set(word)  # letters in the word
     alphabet = set(string.ascii_uppercase)
     used_letters = set()  # what user has guessed

    # getting user input
    while len(word_letters) > 0:
        # letters used
        # ''. join(['a', 'b', ]) --> 'a b '
        print('You have used these letters: ', ' '.join(used_letters))

        # what current word is( W _ R D)
        word_list = [letter if letter in used_letters else '_' for letter in word]
        print('current word: ', ' '.join(word_list))

here is the problem. i cannot give any inputs

        user_letter = input('Guess a letter: ').upper()
        if user_letter in alphabet - used_letters:
            used_letters.add(user_letter)
            if user_letter in word_letters:
                word_letters.remove(user_letter)
        elif user_letter in used_letters:
            print('You have already used that character.Please try again later')

        else:
            print('Invalid character.Please try again later')

''' This is my first project and i'm sorry if this is a silly question. Greatly appreciate if someone can help me.If anyone wants to watch the video. Here is the link

2
  • 2
    did you call main() in your script? Commented Jul 3, 2022 at 12:14
  • no. i thought adding it inside in main function would work when i run the program Commented Jul 5, 2022 at 4:38

1 Answer 1

1

What is wrong...

You only defined the functions (get_valid_word(words) and main()) but you did not called any of the function. Unlike some other language (e.g. java), main() will not be called by default in python, so you will need to call it manually.

What to do...

One way is to put everything inside the def main(): out without defining it as a function. This way the .py script will run every line without the need to explicitly calling it.

# def main():
#    print('do something')
print('do something')

However, the simplest way is to just add main() at the end of your script to call it.

def main():
   print('do something')
main()
Sign up to request clarification or add additional context in comments.

1 Comment

thank you so much for your answer. It explains a lot

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.