Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
510 views
in Technique[技术] by (71.8m points)

syntax - SyntaxError while trying to do my hangman game in Python

I'm trying to make a Hangman game using a TextFile as database for the words (but that is not why I am here for) I'm trying to execute my script to start the game, so I can try it but I keeps throwing me the following SyntaxError:

SyntaxError: unexpected character after line continuation character

here is the code:

import os
import random

#creation of the program
if not os.path.exists('hangman_folder'):
    print("folder created in dir: " .format(os.getcwdb()))
    os.makedirs('hangman_folder')
    os.chdir('hangman_folder')
else:
    print("folder already on file")
    os.chdir('hangman_folder')

#words database
words_list = ["love", "crazy", "man", "natural", "girl"]

#function that is going to sort all the interesting words
# def detect_words():

def check_if_char(theChar, theWord):
    howManyChars = len(theWord)
    splitWord = list(theWord)
    attempts = 0
    success = 0

    for i in range(howManychars):
        position = i + 1
        if theChar == splitWord[position]:
            return position
            print("yes! the letter is in the word ")
        else:
            attempts += 1
            if attempts >= howManyChars:
                failing = True

def hangItUp(hp):
    hp -= 1
    print("Your character is not on the string. Hanging him up!")
    print("{} attempts remaining " .format(hp))
    if hp <= 0:
        userResponse = input("Game Over! Waiting for your response...")
        if userResponse == "try again":
            print("restarting the game.")
        else:
            print("Bye bye...")

def discover_the_word(usedChar, wordToDiscover, blankList):
    revealedList = blankList
    wordSplitted = list(wordToDiscover)
    for letter in wordSplitted:
        if usedChar == letter:
            revealedList[revealedList.index(letter)] = usedChar

    return revealedList

def listToString(s):
    str1 = ""
    for element in s:
        str1 += element

    return str1

#the entire game
def hangman_game():
    #game starting, setting up the variables
    lives = 6
    selectedWord = random.choice(words_list) #selecting a random word
    countBlankSpaces = len(selectedWord)
    blankSpaces = []

    #creating the blank spaces to guess the word
    for i in range(countBlankSpaces):
        blankSpaces.append("_")

    yourWord = input("guess with a word")
    if check_if_char(yourWord, selectedWord) == True:
        blankSpaces = discover_the_word(yourWord, selectedWord, blankSpaces)
        if listToString(blankSpaces) == selectedWord:
            print("Congratulations! You win the Game")
    else:
        hangItUp(lives)

#checking in wich directory are we
print(os.getcwdb())
hangman_game()

I finished this piece of code and tried to run it but I just can't find that "unexpected character after line continuation character", I just can't find in which line the error is.

question from:https://stackoverflow.com/questions/65838843/syntaxerror-while-trying-to-do-my-hangman-game-in-python

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You may change your IDE or something to get more information of your error. Mine(VScode) gives an error message of invalid syntax (<unknown>, line 32). There us no operator like => in Python.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...