它是在缩进(我认为)Python

时间:2014-05-08 03:34:58

标签: python

这是一个有点像Boggle游戏的程序。它掷了16个骰子,然后你必须从滚动的字母中挑出单词。不太喜欢连接骰子,只是给定字母中长度超过3的单词。它根据单词列表检查单词并确保您不重复

这仍然是范围的基础,我仍然是Python 3.3.4的新手。

我认为错误在于get_guesses()中循环的缩进。我试过在不同的循环周围移动循环工作。

import random
global roll
global rolled
global guesses
global word_list
global start
roll = []
rolled = []
word_list = []
guesses = []

# Setting the dice
die0 = ['r', 'i', 'f', 'o', 'b', 'x']
die1 = ['i', 'f', 'e', 'h', 'e', 'y']
die2 = ['d', 'e', 'n', 'o', 'w', 's']
die3 = ['u', 't', 'o', 'k', 'n', 'd']
die4 = ['h', 'm', 's', 'r', 'a', 'o']
die5 = ['l', 'u', 'p', 'e', 't', 's']
die6 = ['a', 'c', 'i', 't', 'o', 'a']
die7 = ['y', 'l', 'g', 'k', 'u', 'e']
die8 = ['qu', 'b', 'm', 'j', 'o', 'a']
die9 = ['e', 'h', 'i', 's', 'p', 'n']
die10 = ['v', 'e', 't', 'i', 'g', 'n']
die11 = ['b', 'a', 'l', 'i', 'y', 't']
die12 = ['e', 'z', 'a', 'v', 'n', 'd']
die13 = ['r', 'a', 'l', 'e', 's', 'c']
die14 = ['u', 'w', 'i', 'l', 'r', 'g']
die15 = ['p', 'a', 'c', 'e', 'm', 'd']
dice = [die0, die1, die2, die3, die4, die5, die6, die7, die8, die9, die10, die11, die12, die13, die14, die15]

# Importing word list and file handling
def dict():
    game_list = open('TWL06.txt')
    for line in game_list:
        line=line.strip()
        word_list.append(line)
    game_list.close()

# Randomly gets a roll from the dice
def dice_roll():
    global roll
    global rolled
    for die in dice:
        roll.append(random.choice(die))
        rolled = ' '.join(roll)

# breaks the rolled dice into 4 groups of 4 
def game_grid():
    print (rolled[0:8])
    print (rolled[8:16])
    print (rolled[16:24])
    print (rolled[24:32])

    get_guesses()

# Get player input for guessed words
def get_guesses():
    global word_list
    global rolled
    guess = input("Enter a word you found.").upper()
    guess_list = list(guesses)
    if (len(guess)) <= 3:
        print("That word is to short.")
        get_guesses()
    else:
        if guess in word_list:
            for i in guess_list:
                if i not in rolled:
                    print("That word is not in the dice.")
                    get_guesses()
            if guess not in guesses:
                print("That is a good word.")
                guesses.append(guess)
                get_guesses()
            else:
                print("You already guessed that one.")
                get_guesses()
        else:
            print("That is not one of the ", len(word_list), "words I know.")
            get_guesses()

# Main menu
def main():
    print("This game is similar to Boggle")
    print("Your goal is to guess as many words from the rolled dice as you can.")
    print("You can use any letters in the grid to play.")
    print("Your words must be 3 letters or longer.")
    play_quit = input("Press 'p' to play or any other key to quit.").lower()
    if play_quit == "p":
        dict()
        dice_roll()
        game_grid()

    else:
        print("Thanks for playing.")

main()

1 个答案:

答案 0 :(得分:0)

据推测,您希望guess_list成为用户访客中字符的列表,但实际上您将其设置为guesses的副本,即用户之前单词的列表。

此外,您正在将用户的输入转换为大写,但您随机选择的字母是小写的,因此无论如何您永远不会在rolled中找到用户输入的字符。我不知道TWL06.txt的内容使用了什么案例。

此计划中的许多名称都可以改进。

你有一个名为dict的函数,这是一个坏主意,因为dict是一个创建字典的内置函数。

您有一个名为game_list的变量,它是一个文件,而不是一个列表。

你有一个名为word_list的变量,这是一个列表,但words会更简洁,同样有用。

您有一些名为die0die1die2等的变量,这些变量不是必需的。您可以使用嵌套列表文字创建dice

名为i的迭代变量意味着一个整数,但您打算将其作为一个字符。

此外,您在很多不需要它的地方使用global。当您要分配给全局变量并且赋值语句在函数中时,您只需要它。当您在全局变量上调用append时,它不会分配给变量。

import random

acceptable_words = []
acceptable_letters = []
user_words = []

dice = [
    ['r', 'i', 'f', 'o', 'b', 'x'],
    ['i', 'f', 'e', 'h', 'e', 'y'],
    ['d', 'e', 'n', 'o', 'w', 's'],
    ['u', 't', 'o', 'k', 'n', 'd'],
    ['h', 'm', 's', 'r', 'a', 'o'],
    ['l', 'u', 'p', 'e', 't', 's'],
    ['a', 'c', 'i', 't', 'o', 'a'],
    ['y', 'l', 'g', 'k', 'u', 'e'],
    ['qu', 'b', 'm', 'j', 'o', 'a'],
    ['e', 'h', 'i', 's', 'p', 'n'],
    ['v', 'e', 't', 'i', 'g', 'n'],
    ['b', 'a', 'l', 'i', 'y', 't'],
    ['e', 'z', 'a', 'v', 'n', 'd'],
    ['r', 'a', 'l', 'e', 's', 'c'],
    ['u', 'w', 'i', 'l', 'r', 'g'],
    ['p', 'a', 'c', 'e', 'm', 'd']]

def load_acceptable_words():
    with open('TWL06.txt') as word_file:
        for line in word_file:
            acceptable_words.append(line.strip().lower())

def roll_dice():
    for die in dice:
        acceptable_letters.append(random.choice(die))

def print_game_grid():
    for i in range(4):
        print_game_row(i)

def print_game_row(i):
    print (' '.join(acceptable_letters[i:i+4]))

def run_game_loop():
    while True:
        run_one_turn()

def run_one_turn():
    candidate = input("Enter a word you found.").strip().lower()
    if (check_is_long_enough(candidate)
        and check_can_be_spelled(candidate)
        and check_is_acceptable_word(candidate)):
        accept_user_word(candidate)

def check_is_long_enough(candidate):
    if len(candidate) >= 3:
        return True
    else:
        print("That word is to short.")
        return False

def check_can_be_spelled(candidate):
    if all(letter in acceptable_letters for letter in candidate):
        return True
    else:
        print("That word is not in the dice.")
        return False

def check_is_acceptable_word(candidate):
    if candidate in acceptable_words:
        return True
    else:
        print("That is not one of the ", len(acceptable_words), "words I know.")
        return False

def accept_user_word(word):
    user_words.append(word)

def main():
    print("This game is similar to Boggle.")
    print("Your goal is to guess as many words from the rolled dice as you can.")
    print("You can use any letters in the grid to play.")
    print("Your words must be 3 letters or longer.")
    play_quit = input("Press 'p' to play or any other key to quit.").strip().lower()
    if play_quit == "p":
        load_acceptable_words()
        roll_dice()
        print_game_grid()
        run_game_loop()
    else:
        print("Thanks for not playing.")

main()