简单的子手游戏的实现

时间:2019-01-18 12:56:49

标签: python python-3.x

我需要用希腊字母实现我自己的经典子手游戏版本。

我已经尝试过几次修改代码,但是似乎在管理所有的“ if”和“ else”语句时遇到了问题。我的主要问题是我无法正确设置新单词,以便用户每次猜测正确时都可以看到(get_word函数)。这是我的代码:

import random

words=['''list of greek words''']

greek_alphabet=['''list of all greek letters''']

def shape(left=5):
    if left==5:
        print('|----------|')
        print('|          O')
        print('|')
        print('|')
        print('|')
        print('|')
        print('|')
    elif left==4:
        print('|----------|')
        print('|          O')
        print('|         /|\\')
        print('|')
        print('|')
        print('|')
        print('|') 
    elif left==3:
        print('|----------|')
        print('|          O')
        print('|         /|\\')
        print('|          |')
        print('|')
        print('|')
        print('|')
    elif left==2:
        print('|----------|')
        print('|          O')
        print('|         /|\\')
        print('|          |')
        print('|        _/ \\_')
        print('|')
        print('|')
    elif left==1:
        print('|----------|')
        print('|          O')
        print('|         /|\\')
        print('|          |')
        print('|        _/ \\_')
        print('|        ## ##')
        print('|')
    else:
        print('|----------|')
        print('|          O')
        print('|         /|\\')
        print('|          |')
        print('|        _/ \\_')
        print('|        ## ##')
        print('|         fire')

def get_word(random_word,letter):
    for i in random_word:
        if i==letter:
            new_word.replace('_ ',letter)
    return new_word  

def hangman():
    found=False
    random_word=random.choice(words)
    words.remove(random_word)
    used_letters=[]
    max_guesses=0
    incorrect_guesses=0
    new_word=len(random_word)*'_ '
    print('You can make up to 5 mistakes')
    print('The 6th mistake is going to get you out of the game')
    print('The word you must guess is: ',new_word)
    while not found and incorrect_guesses<max_guesses:
        letter=input('Give letter: ')
        if found_word(random_word,new_word):
            print('Congrats! You found the word!')
            found=True
        elif letter in random_word and letter not in used_letters:
            used_letters.append(letter)
            print('The word you must guess is',get_word(random_word,letter))
        elif letter not in greek_alphabet:
            print('You did not give a letter. Try again.')
        elif letter in used_letters:
            print('This letter has already been used')
        else:
            incorrect_guesses+=1
            left=max_guesses-incorrect_guesses
            shape(left)
            print('You still have ',max_guesses-incorrect_guesses,'lives')
            print('The word you must choose is ',get_word(random_word,letter))

    if not found:
        shape()
        print('You did not find the word')
        print('The word we were looking for was ',random_word)
        return False
    else:
        return True

def found_hidden_word(random_word,new_word):
    if new_word==random_word:
        return True

hangman()

我已经尽力做到这一点,因为我绝对是一个初学者,但是我相信经过一些更改,我的代码才能正常工作。

2 个答案:

答案 0 :(得分:0)

您必须将new_word传递给函数get_word才能在此处使用它。替换时,您只是在创建一个新变量new_word。

可以:

def get_word(random_word, new_word, letter)

(不要忘记在hangman方法中将此返回的属性再次归因于new_word)

OR
 声明为全局(不是很好): 在顶部:

new_word = ""

在使用它的功能中:

global new_word

要求提供完整代码:

import random

words=['''list of greek words''']

greek_alphabet=['''list of all greek letters''']

def shape(left=5):
    if left==5:
        print('|----------|')
        print('|          O')
        print('|')
        print('|')
        print('|')
        print('|')
        print('|')
    elif left==4:
        print('|----------|')
        print('|          O')
        print('|         /|\\')
        print('|')
        print('|')
        print('|')
        print('|') 
    elif left==3:
        print('|----------|')
        print('|          O')
        print('|         /|\\')
        print('|          |')
        print('|')
        print('|')
        print('|')
    elif left==2:
        print('|----------|')
        print('|          O')
        print('|         /|\\')
        print('|          |')
        print('|        _/ \\_')
        print('|')
        print('|')
    elif left==1:
        print('|----------|')
        print('|          O')
        print('|         /|\\')
        print('|          |')
        print('|        _/ \\_')
        print('|        ## ##')
        print('|')
    else:
        print('|----------|')
        print('|          O')
        print('|         /|\\')
        print('|          |')
        print('|        _/ \\_')
        print('|        ## ##')
        print('|         fire')

def get_word(random_word, new_word, letter):
    for i in random_word:
        if i==letter:
            new_word.replace('_ ',letter)
    return new_word  

def hangman():
    found=False
    random_word=random.choice(words)
    words.remove(random_word)
    used_letters=[]
    max_guesses=0
    incorrect_guesses=0
    new_word=len(random_word)*'_ '
    print('You can make up to 5 mistakes')
    print('The 6th mistake is going to get you out of the game')
    print('The word you must guess is: ',new_word)
    while not found and incorrect_guesses<max_guesses:
        letter=input('Give letter: ')
        if found_hidden_word(random_word,new_word):
            print('Congrats! You found the word!')
            found=True
        elif letter in random_word and letter not in used_letters:
            used_letters.append(letter)
            new_word = get_word(random_word, new_word, letter)
            print('The word you must guess is', new_word)
        elif letter not in greek_alphabet:
            print('You did not give a letter. Try again.')
        elif letter in used_letters:
            print('This letter has already been used')
        else:
            incorrect_guesses+=1
            left=max_guesses-incorrect_guesses
            shape(left)
            print('You still have ',max_guesses-incorrect_guesses,'lives')
            new_word = get_word(random_word,letter)
            print('The word you must choose is ',new_word)

    if not found:
        shape()
        print('You did not find the word')
        print('The word we were looking for was ',random_word)
        return False
    else:
        return True

def found_hidden_word(random_word,new_word):
    if new_word==random_word:
        return True

hangman()

答案 1 :(得分:0)

我可以建议以下内容作为起点?请注意,您需要提供自己的words.txt文件,每行一个希腊字。一旦该文件位于同一目录中,此程序便可以正常运行:

#! /usr/bin/env python3
import random
import sys
import turtle


def main():
    words = load_words()
    my_word = random.choice(words)
    print('Welcome to the game of hangman!')
    print('I have chosen a word for you to guess,')
    print(f'and it contains {len(my_word)} letters.')
    print('Here is the gallows for your hangman.')
    hangman = draw_hangman()
    next(hangman)
    guess = [None] * len(my_word)
    wrong = []
    while True:
        print('This is what you have so far.')
        print(' '.join('_' if char is None else char for char in guess))
        char = get_character()
        if char in guess or char in wrong:
            print(f'You have already guessed {char!r} previously.')
        elif char in my_word:
            for index, my_char in enumerate(my_word):
                if my_char == char:
                    guess[index] = my_char
            if None not in guess:
                print('You have won!')
                print(f'You have guessed {my_word!r} correctly!')
                draw_finish()
        else:
            wrong.append(char)
            print('Wrong!')
            next(hangman)


def get_character():
    while True:
        try:
            char = input('What character would you like to guess? ')
        except EOFError:
            sys.exit()
        except KeyboardInterrupt:
            print('Please try again.')
        else:
            if len(char) != 1:
                print('Only a single character can be accepted.')
            elif not char.isalpha():
                print('Please only use letters from the alphabet.')
            else:
                return char.casefold()


def load_words():
    with open('words.txt') as file:
        return [word.casefold()
                for word in map(str.strip, file)
                if word.isalpha() and len(word) >= 5 and len(set(word)) > 1]


def draw_hangman():
    # Draw the gallows.
    turtle.mode('logo')
    turtle.speed(0)
    turtle.reset()
    turtle.penup()
    turtle.hideturtle()
    turtle.goto(300, -300)
    turtle.pendown()
    turtle.goto(-300, -300)
    turtle.goto(-300, 200)
    turtle.goto(-200, 300)
    turtle.goto(0, 300)
    turtle.color('brown')
    turtle.goto(0, 200)
    yield
    # Draw the head.
    turtle.color('green')
    turtle.left(90)
    turtle.circle(50)
    yield
    # Draw the body.
    turtle.penup()
    turtle.left(90)
    turtle.forward(100)
    turtle.pendown()
    turtle.forward(200)
    yield
    # Draw left arm.
    turtle.penup()
    turtle.backward(150)
    turtle.right(45)
    turtle.pendown()
    turtle.forward(100)
    yield
    # Draw right arm.
    turtle.penup()
    turtle.backward(100)
    turtle.left(90)
    turtle.pendown()
    turtle.forward(100)
    yield
    # Draw left leg.
    turtle.penup()
    turtle.backward(100)
    turtle.right(45)
    turtle.forward(150)
    turtle.right(22.5)
    turtle.pendown()
    turtle.forward(150)
    yield
    # Draw right leg.
    turtle.penup()
    turtle.backward(150)
    turtle.left(45)
    turtle.pendown()
    turtle.forward(150)
    turtle.done()


def draw_finish():
    turtle.penup()
    turtle.goto(0, 0)
    turtle.write('You won!')
    turtle.done()


if __name__ == '__main__':
    main()