1
\$\begingroup\$

I have been playing around with python and made myself a hangman game.

Questions:

  1. How readable my code is?

  2. Are there any logical errors that I'm not aware of?

  3. How to improve it?

  4. Is there a better way to approach the problem?

  5. Overall, how would you rate it out of 10?


import json, random, urllib.request


def get_word():
    url = urllib.request.urlopen("https://raw.githubusercontent.com/sindresorhus/mnemonic-words/master/words.json")
    words = json.loads(url.read())
    random_word = random.choice(words)
    return random_word


saved_word = []
word = get_word()
saved_word = word
length = len(word)
tries = 4


def drawing(t):
    draw = {0: '''  
                    ----------
                    |        |
                    |        O
                    |       /|\\
                    |       / \\
                    |
                    -----
                ''',
            1: '''  
                    ----------
                    |        
                    |        
                    |        O
                    |       /|\\
                    |       / \\
                    -----
                ''',
            2: '''  

                    |        
                    |        
                    |        O
                    |       /|\\
                    |       / \\
                    -----
            ''',
            3: '''  


                             O
                            /|\\
                            / \\
                    -----
                ''',
            4: '''  




                             O
                            /|\\
                            / \\
                '''
            }
    d = draw[t]
    print(d)


def what_range(width):  # checks how many letters should be covered depending on the length of the word
    if width <= 3:
        return 2
    elif 3 < width < 6:
        return 3
    else:
        return 4


def mixer(w):  # creates the blanks on the word
    m = ""
    item = list(w)
    width = what_range(length)
    for z in range(width):
        n = random.randint(0, length - 1)
        if str(n) in m:
            while str(n) in m:
                n = random.randint(0, length - 1)
        if str(n) not in m:
            m = m + str(n)
            item.pop(n)
            item.insert(n, "_")
    print("".join(item))
    return "".join(item)


def find_letter(numbers, item, letter):
    i = list(item)
    for number in numbers:
        i.pop(number)
        i.insert(number, letter)
    return "".join(i)


word = mixer(word)
completed = False
while not completed:
    letter = input("enter a letter: ")
    n = 0
    nums = []
    if letter in saved_word:
        for l in saved_word:
            if l == letter:
                nums.append(n)
            n += 1
    else:
        tries -= 1
    print(f"Tries: {tries}")
    drawing(tries)
    if tries != 0:
        word = find_letter(nums, word, letter)
        print(word)
        if word == saved_word:
            print("You WIN!")
            completed = True
    else:
        print(f"You FAILED!\nThe word was {saved_word}")
        exit()
\$\endgroup\$
  • \$\begingroup\$ Please change your question title to a brief description what your code does. \$\endgroup\$ – πάντα ῥεῖ May 3 at 17:25
  • \$\begingroup\$ Have you tested the code and does it work as expected? Are there any logical errors that I'm not aware of? \$\endgroup\$ – pacmaninbw May 3 at 18:28
  • \$\begingroup\$ Sorry, I have tested the code and it does work as I expected it, but would like to know if there are any other ways I can improve my coding, or is it readable. \$\endgroup\$ – gamby098 May 3 at 18:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.