如果我玩了一轮游戏并选择退出选项,则游戏结束,但是,如果我玩第二轮并尝试退出,则游戏继续并提示用户再次输入猜测,而不是终止游戏。
似乎陷入了困境。
这是我的代码:
from random import randint
def core_game():
def init_hangman():
hangman = []
for x in range(7):
hangman.append([" "] * 7)
hangman[0][0] = "_"
hangman[0][1] = "_"
hangman[0][2] = "_"
hangman[1][3] = "|"
return hangman
hangman = init_hangman()
def print_hangman():
for x in hangman:
print(str.join("", x))
def get_input(guess):
your_guess = input(guess)
if your_guess in guessed_letters:
print("You already guessed that letter!")
return get_input(guess)
elif your_guess.isalpha() and len(your_guess) == 1:
return your_guess
else:
print("Please guess a single letter!")
return get_input(guess)
words_list = ["monkey", "cow"]
city_list = ["Amarillo", "Houston"]
lists = ["animals", "cities"]
random_list = randint(0,1)
random_word = randint(0,1)
if lists[random_list] == "cities":
rand_list = city_list
elif lists[random_list] == "animals":
rand_list = words_list
word = rand_list[random_word]
guessed = ""
guessed_letters = []
hang = 6
Cont = True
for letter in word:
guessed += "-"
print("\n\n\nWELCOME TO HANGMAN!")
print("The category is: ", lists[random_list])
print("The secret word: ", guessed, "is", len(guessed), "letters")
while Cont:
your_guess = get_input("\nEnter your guess: ")
if your_guess in word.lower():
for x in range(len(word)):
if word[x].lower() == your_guess.lower():
guessed = guessed[:x] + word[x] + guessed[x+1:]
guessed_letters.append(your_guess)
print("\nThe secret word: ", guessed)
if guessed.lower() == word.lower():
print("\n\nCongratulations, you guessed the word!")
play_again = input("\nWould you like to play again?(y/n) ")
if play_again == "y" or play_again == "yes":
core_game()
else:
Cont = False
else:
hang -= 1
guessed_letters.append(your_guess)
print("\nGuessed letters: ", guessed_letters)
if hang == 5:
hangman[2][3] = "O"
print_hangman()
print(guessed)
elif hang == 4:
hangman[3][3] = "|"
print_hangman()
print(guessed)
elif hang == 3:
hangman[3][2] = "-"
print_hangman()
print(guessed)
elif hang == 2:
hangman[3][4] = "-"
print_hangman()
print(guessed)
elif hang == 1:
hangman[4][2] = "/"
print_hangman()
print(guessed)
elif hang == 0:
hangman[4][4] = "\\"
print_hangman()
print("Game Over!")
print("The word was: ", word)
play_again = input("Would you like to play again?(y/n) ")
if play_again == "y" or play_again == "yes":
core_game()
else:
Cont = False
core_game()
主要功能是core_game(),并在程序运行时调用它。
答案 0 :(得分:0)
好的 - 是的,我喜欢这款游戏 - 在浏览器中播放trinket.io
然后我走了整个游戏here。使用它来逐步调试代码,您可以查看所有变量,列表等,以及输入输入和视图输出。
你的游戏重复的原因是:
1)在函数 core_game 中,您有以下代码:
if play_again == "y" or play_again == "yes":
core_game()
这意味着当您在第一个游戏之后选择“是”时,您将打开该功能的另一个“实例” - 如果您浏览代码,则可以看到此实例。您也可以在下面的屏幕截图(f19)中看到这一点。
2)在你玩第二个游戏后(记住第一个实例仍然打开)并选择“n”,这会导致“Cont”变量更改为False,但仅在第二个实例中。然后关闭“while”,代码返回到Cont仍为True的第一个实例。您还可以在屏幕截图中看到这两个实例,以及“Cont”的每个值的状态。封闭的框架是“灰色的”。
3)程序在while循环开始时在core_game的第一个实例中恢复,并提示用户输入猜测。
在您浏览代码之前很难看到,但我在这里包含了一个快照,您可以(我希望)看到我的意思。
所以问题是你从内部调用函数。
考虑更像这样的事情:
def core_game():
print("I was called")
while Cont:
ans = ("Do you wish to play a game? (Y/N)")
if ans[0].lower() == "y": # works for Y, y, Yup, Yeah, Yes etc.
core_game()
else:
Cont = False
PS 还有其他非常好的参考资料来源,例如this。
永远不要放弃 - 你会发现步入式非常方便,你可以在稍后的阶段尝试Rice Course MOOC on Coursera。