【问题标题】:Hangman game in Python - my break seems to only work every OTHER time the game is playedPython 中的刽子手游戏 - 我的休息时间似乎只在每隔一次玩游戏时才有效
【发布时间】:2017-07-02 22:04:09
【问题描述】:

正如标题所示,游戏运行良好,但似乎我有一个我无法弄清楚的错误。

在第一局之后,当它问你是否想再玩时,它会继续下一个游戏,你可以玩那个,然后在那个游戏之后,它不会问你是否想玩再玩一次,但会自动开始一个新游戏,使用上一个游戏中刚刚出现的单词。

我不知道是我的 play_again 函数还是我的 check_win 函数还是什么。

感谢您的帮助

import random

def main():

    hangman_word = generate_random()

    while True:
        play_game(hangman_word)

        if play_again() == 2:
            return False
            break
        else:
            hangman_word = generate_random()            
            play_game(hangman_word)


############################################
# The game!
############################################

def play_game(random_word):

    clear()
    guesses = ""
    tries = 9

    while True:
        winning_number = 0
        hangmanInterface(tries)
        print("WORD:", end="")
        for char in random_word:
            if char in guesses:
                print(" {} ".format(char), end="")
            else:
                print(" _ ", end="")
                winning_number += 1

        if win_check(winning_number, tries) == True:
            break

        print_x(1)

        print("    Tried:", end="")
        for j in range(len(guesses)):
            print("[{}]".format(guesses[j]), end="")
        spacing_fix(2)
        guess = get_guess()
        guesses += guess

        print_x(2)

        if guess not in random_word:
            tries -= 1
    return
##########################################


##########################################
# generates a random word
##########################################
def generate_random():

    #opens the dictionary and initializes a random word
    with open("dict.txt") as fp:
        dictwords = []
        for line in fp:
            dictwords.append(line.rstrip("\n"))

    #makes sure the word is in lower case        
    rand = random.choice(dictwords)
    randlower = rand.lower()
    return randlower


##########################################





##########################################
# Checks if the game should restart
##########################################
def play_again():

    print("Play again?")
    print("1. yes!")
    print("2. no :(")
    ans = get_ans()
    return ans

##########################################
def get_ans():
    ans1 = input()
    ans = int(ans1)
    if ans == 1 or ans == 2:
        return ans
    else:    
        print("Please type 1 or 2")
        get_ans()

##########################################





##########################################
# checks for the win
########################################## 
def win_check(a, b):
    if a == 0:
        print_x(2)
        print("######################")
        print("#                    #")
        print("#    W I N N E R     #")
        print("#                    #")        
        print("######################")        
        return True
    elif b == 0:
        print_x(2)
        print("######################")
        print("#                    #")
        print("#     L O S E R      #")
        print("#                    #")        
        print("######################")  
        return True
##########################################   




##########################################
# gets users guess while ensuring only
# one alpha, lowercase char is entered
########################################## 
def get_guess():
    get = input("    Guess: ")
    a = get.lower()
    if len(a) > 1:
        print("One letter only")
        get_guess()
    elif not a.isalpha():
        print("One letter only")
        get_guess()

    return a
##########################################   




##########################################
# Aesthetic Functions
##########################################
def clear():
    for i in range(25):
        print ('\n')

#########################
def print_x(x):
    for i in range(x):
        print("\n")


#########################
def spacing_fix(tmp):
    if tmp == 0:
        tmp = 1
        return tmp
    if tmp == 2:
        print()
        tmp = 3
        return tmp
##########################################


##########################################
# prints board state
########################################## 
def hangmanInterface(index):
        if index==0:
            clear()
            print('          _____ ')
            print('          |   | ')
            print('          O   | ')
            print('         /|\  | ')
            print('         / \  | ')
            print('              | ')
            print('      ________|_')
            return
        if index==1:
            clear()
            print('          _____ ')
            print('          |   | ')
            print('          O   | ')
            print('         /|\  | ')
            print('         /    | ')
            print('              | ')
            print('      ________|_')
            return
        if index==2:
            clear()
            print('          _____ ')
            print('          |   | ')
            print('          O   | ')
            print('         /|\  | ')
            print('              | ')
            print('              | ')
            print('      ________|_')
            return
        if index==3:
            clear()
            print('          _____ ')
            print('          |   | ')
            print('          O   | ')
            print('         /|   | ')
            print('              | ')
            print('              | ')
            print('      ________|_')
            return
        if index==4:
            clear()
            print('          _____ ')
            print('          |   | ')
            print('          O   | ')
            print('          |   | ')
            print('              | ')
            print('              | ')
            print('      ________|_')
            return
        if index==5:
            clear()
            print('          _____ ')
            print('          |   | ')
            print('          O   | ')
            print('              | ')
            print('              | ')
            print('              | ')
            print('      ________|_')
            return
        if index==6:
            clear()
            print('          _____ ')
            print('          |   | ')
            print('              | ')
            print('              | ')
            print('              | ')
            print('              | ')
            print('      ________|_')
            return
        if index==7:
            clear()
            print('          _____ ')
            print('              | ')
            print('              | ')
            print('              | ')
            print('              | ')
            print('              | ')
            print('      ________|_')
            return
        if index==8:
            clear()
            print('                ')
            print('              | ')
            print('              | ')
            print('              | ')
            print('              | ')
            print('              | ')
            print('      ________|_')
            return
        if index==9:
            clear()
            print('                ')
            print('                ')
            print('                ')
            print('                ')
            print('                ')
            print('                ')
            print('      ________|_')
            return

##########################################  


if __name__ == "__main__":
    main()

【问题讨论】:

  • 如果您觉得有帮助,请考虑接受答案

标签: python function break game-loop


【解决方案1】:

问题是这个循环:

while True:
    play_game(hangman_word)

    if play_again() == 2:
        return False
        break
    else:
        hangman_word = generate_random()            
        play_game(hangman_word)

此循环以play_game(...) 开始和结束,因此您可以让用户玩两个游戏而无需在其间要求输入。一个电话play_game 就足够了。

【讨论】:

  • 谢谢,我不知道我是怎么错过的!感谢您的帮助
【解决方案2】:

而不是这个:

def main():
    hangman_word = generate_random()
    while True:
        play_game(hangman_word)

        if play_again() == 2:
            return False
            break
        else:
            hangman_word = generate_random()            
            play_game(hangman_word)

试试这个:

def main():
    while True:
        hangman_word = generate_random() ## get ONE word
        play_game(hangman_word)          ## play ONE game

        if play_again() == 2:  ## if user doesn't want to play anymore, quit
            break ## you could also return False,
            ## just don't do both because only one will be called

您在原版中所做的事情: 当main() 第一次被调用时,你已经玩了零游戏。 在while True: 正下方的第一个play_game(...) 处,您正在玩​​您的第一个游戏(游戏1)。之后,如果 play_again() 为 1,则您正在玩第二个刽子手游戏(在 else: 子句下)。在这两个游戏之后,您循环回到顶部并再次在while True: 下玩游戏。

实际上,原版只能玩奇数个游戏(1、3、5、7、...)。

【讨论】:

  • 谢谢,我不知道我是怎么错过的!感谢您的帮助
猜你喜欢
  • 2010-10-28
  • 2014-10-19
  • 2015-05-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-08-27
  • 1970-01-01
相关资源
最近更新 更多