Jul-17-2018, 11:45 AM
Quote: @Windspar: what do you mean by "top code"? the import parts?example
### THE HANGMAN - MAIN ### import re import pickle from random import randrange from hangmanFunctions import * #custom module def main(): nextStep = False while not nextStep: usrName = input("Welcome to the Hangman's game, please enter your name:\n") print("Your name is {}, correct? Y/N.\n".format(usrName)) answ = input("- ") validAnsw = isLetter(answ) if validAnsw and answ == "y": nextStep = True elif validAnsw and answ == "n": print("Okay, let's try again.\n") else: print("You should enter a single letter. Let's try again.\n") #open pickledDict, if not there, create file with empty dict try: userScoresDict = unpickleObj() except FileNotFoundError: userScoresDict = {} pickleObj(userScoresDict) #check if user exists in dict, if not creates entry usrInDict = keyInDict(usrName, userScoresDict) if usrInDict: print("You've already been playing and your currents score is {}.\n".format( userScoresDict[usrName])) else: print("This is the first time you're playing: welcome!\n") userScoresDict[usrName] = 0 #adding usr to dict score = userScoresDict[usrName] #creating score var print("You will be given a word and must guess it in less than 8\ strokes. Ready? let's start!\n") #game loop starts # ctd = True while ctd: secretW = generateWord() #picking random word secretW = secretW.strip() #creating the current word with "*" for each letter i = 1 currentW = "*" while i < (len(secretW)): currentW += "*" i += 1 print("\t{}-letter word : {}\n".format((len(secretW)), currentW)) #guessing part for user strokes = 1 found = False while not found and (strokes <= 8): usrInput = input("- ") if len(usrInput) == len(secretW): currentW = usrInput currentW = currentW.strip() #in case there's any extra space if currentW == secretW: found = True print("Congratulations, you win!") else: print("Sorry, maybe that was too soon.\n") elif re.search(usrInput, secretW) is not None: #if anything matches currentW = matchingLW(usrInput, currentW, secretW) print("{}\n".format(currentW)) if currentW == secretW: found = True print("Congratulations, you win!") elif re.match(usrInput, secretW) is None: print("Nope, sorry") if strokes == 8: print("Sorry, 8 strokes, game over.\n\The word was {}.".format(secretW)) strokes += 1 if (strokes == 8) and not found: score -= 5 #losing points for not finding else: score += (8-strokes) #handling the score #asking if usr wishes to continue playing or not gotAnsw = False while not gotAnsw: print("So, {}, would you like to play again? Y/N\n".format(usrName)) answ = input("- ") answ = answ.casefold() if answ == "y": ctd = True gotAnsw = True elif answ == "n": ctd = False gotAnsw = True else: print("Sorry, couldn't read your answer, let's try again.\n") #saving current player's score to dict userScoresDict[usrName] = score print("Your score is now {}. See you soon.\n".format(userScoresDict[usrName])) pickleObj(userScoresDict) #saves current dict to file main()
99 percent of computer problems exists between chair and keyboard.