Jul-06-2020, 09:56 PM
import random import sys import time # generate random word list_w = ["broad", "apple", "banana", "strawberry"] # the blanks equal the length of the word # list_space = ["-"] * len(word) # create the function for the game def random_word(): global word global list_space1 global guessed_letters global wrong_guesses word = random.choice(list_w) list_space1 = ["-"] * len(word) guessed_letters = [] wrong_guesses = [] print("".join(list_space1)) #if "-" not in list_space1: # print("You have guessed the word") def run(): if "-" not in list_space1: print("You have guessed the word!\n") # ask if you want to close the programm ask = input("Do you want to play again?\n") # you call the random function again because it was called once in # the beginning. if ask == "Yes" or ask == "yes": random_word() elif ask == "no" or ask == "No": time.sleep(1) sys.exit() answer = input("Guess a letter: \n") # check if answer in word # loop through all the numbers in range(len(word)) # if word len = 4 then loop through range(4) # if answer == word[i] # list_space[i] = answer\ # when loop again it checks if you guessed the letter # if so print etc. This shouldnt change the for loop. if answer in guessed_letters: print("You already guessed that!") # if answer is not in the word, add that answer # to wrong guesses elif answer not in word: print("Wrong guess!") wrong_guesses.append(answer) # only if answer not in guessed_letters, execute for loop. elif answer not in guessed_letters and answer not in wrong_guesses: print("It's in the word") for i in range(len(word)): if answer == word[i]: list_space1[i] = answer guessed_letters.append(answer) print("\n") print("".join(list_space1)) # only re-call random_word when you've guessed the word random_word() run_game = True while run_game: run()