Python Forum
Hangman game, feedback appreciated
Thread Rating:
  • 1 Vote(s) - 5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Hangman game, feedback appreciated
#8
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.
Reply


Messages In This Thread
RE: Hangman game, feedback appreciated - by buran - Jul-16-2018, 02:29 PM
RE: Hangman game, feedback appreciated - by Windspar - Jul-17-2018, 11:45 AM
RE: Hangman game, feedback appreciated - by buran - Jul-19-2018, 08:59 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  A hangman game. mcmxl22 1 2,176 Oct-07-2019, 02:02 PM
Last Post: ichabod801
  tk hangman game joe_momma 0 2,956 Aug-09-2019, 02:48 PM
Last Post: joe_momma
  Looking for feedback on a game I made MrPucake 7 4,488 Apr-04-2018, 01:53 PM
Last Post: MIPython

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020