Jul-06-2020, 09:56 PM
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
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() |