【发布时间】:2021-01-22 03:15:35
【问题描述】:
我正在尝试使用 TextFile 作为单词的数据库来制作 Hangman 游戏(但这不是我来这里的原因) 我正在尝试执行我的脚本来启动游戏,所以我可以尝试一下,但我一直向我抛出以下 SyntaxError:
SyntaxError: unexpected character after line continuation character
代码如下:
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 which directory are we
print(os.getcwdb())
hangman_game()
我完成了这段代码并尝试运行它,但是我找不到“行继续字符后的意外字符”,我只是找不到错误在哪一行。
【问题讨论】:
-
显示错误。
-
你让我们猜测错误在哪里。请更新问题以包含 whole 错误回溯消息。
-
那是错误在输出“SyntaxError: 行继续字符后的意外字符”
-
只有当您滥用反斜杠时才会出现该错误消息。您发布的代码不包含一个反斜杠。因此,您实际上并未发布产生该错误的代码。 (发布的代码有 other 错误,但不是那个特定的错误。)