我目前正在尝试制作一个刽子手游戏。我已经在任何函数之外定义了变量“lives”,但是当尝试在 start_game 函数中使用它时,编辑器说该变量已定义但从未使用过。但是,当我尝试将它声明为全局时,无论它是在函数内部还是外部,它都会给我一个“无效语法”错误——特别是在赋值运算符“=”处。
import random
words = "dog cat log hog etc" # <-- just a huge string of a bunch of words
words = words.split()
word = random.choice(words)
# Difficulties: Easy:12 Medium:9 Hard:6
lives = 0
current = "_" * len(word)
def gameLoop():
while current != word and lives > 0:
print("Guess a letter. If you wish to exit the game, enter 'exit'")
input("")
print(lives)
def start_game():
while True:
print("Welcome to Hangman! What game mode would you like to play? Easy, medium, or hard?")
game_mode = str.lower(input(""))
if game_mode == "easy":
lives = 12
gameLoop()
break
elif game_mode == "medium":
lives = 9
gameLoop()
break
elif game_mode == "hard":
lives = 6
gameLoop()
break
start_game()
慕雪6442864
狐的传说
相关分类