Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/python-2.7/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python-从main()函数调用函数返回零_Python_Python 2.7 - Fatal编程技术网

Python-从main()函数调用函数返回零

Python-从main()函数调用函数返回零,python,python-2.7,Python,Python 2.7,我正在编写一个程序,模拟《辐射3》中的黑客迷你游戏。用户应该选择一个难度级别,并根据该级别,程序将返回一个字符串列表,然后用户有4次尝试来正确猜测程序随机选择的单词 我面临的问题是,当我调用我编写的各种函数来处理游戏的不同方面时,没有返回任何值。如果我分别调用每个函数并向其传递所需的参数,我将获得正确的输出,但由于某些原因,当我从main()函数调用它们时,它将不起作用。这是我的密码: import operator import random def difficulty_level(di

我正在编写一个程序,模拟《辐射3》中的黑客迷你游戏。用户应该选择一个难度级别,并根据该级别,程序将返回一个字符串列表,然后用户有4次尝试来正确猜测程序随机选择的单词

我面临的问题是,当我调用我编写的各种函数来处理游戏的不同方面时,没有返回任何值。如果我分别调用每个函数并向其传递所需的参数,我将获得正确的输出,但由于某些原因,当我从main()函数调用它们时,它将不起作用。这是我的密码:

import operator
import random


def difficulty_level(difficulty_choice):
    num_choices = 0
    word_length = 0
    if difficulty_choice == 1:
        num_choices = 5
        word_length = 5
    elif difficulty_choice == 2:
        num_choices = 8
        word_length = 9
    elif difficulty_choice == 3:
        num_choices = 10
        word_length = 13
    elif difficulty_choice == 4:
        num_choices = 12
        word_length = 17
    elif difficulty_choice == 5:
        num_choices = 15
        word_length = 21
    return num_choices, word_length


def generate_word_list(num_choices, word_length):
    matching_words = []
    word_choice_list = []
    word_source = open("enable1.txt", 'r')
    for word in word_source:
        if len(word) == word_length + 1:
            matching_words.append(word.rstrip())
    for i in range(num_choices):
        word_choice_list.append(random.choice(matching_words))
    return word_choice_list


def user_guesses(word_choice_list):
    guesses = 4
    game_over = False

    for word in word_choice_list:
        print word.upper()

    selected_word = random.choice(word_choice_list)
    selected_word_length = len(selected_word)

    while not game_over:
        guess_word = (raw_input("Guess (%s left)? " % guesses)).lower()
        if guess_word == selected_word:
            game_over = True
            print("You win!")
        else:
            num_correct_letters = map(operator.eq, guess_word, selected_word).count(True)
            guesses -= 1
            print("%s/%s correct" % (num_correct_letters, selected_word_length))


def main():
    game_level = raw_input("Difficulty (1-5)? ")
    main_num_choices, main_word_length = difficulty_level(game_level)
    word_list = generate_word_list(main_num_choices, main_word_length)
    user_guesses(word_list)

main()

我不知道为什么它不起作用。我用PyCharm编写这篇文章,并使用2.7.6作为编译器。谢谢。

这对我来说很好用。我想你的问题可能是你希望
游戏级别
int
,但实际上是
str
。这使得
难度\u level
返回了错误的东西(它总是返回
(0,0)
),这会导致整个程序的失败。您可以非常轻松地解决此问题:

game_level = int(raw_input("Difficulty (1-5)? "))
此外,在打开“enable1.txt”之后,您不会关闭它。我建议像这样打开它,使其自动关闭:

with open("enable1.txt", 'r') as word_source:
    for word in word_source:
        if len(word) == word_length + 1:
            matching_words.append(word.rstrip())

我觉得你问的有点不清楚。你能再解释一下到底发生了什么吗?
main中的原始输入
返回一个字符串。您正在将此字符串传递到难度_级别。因此,它返回的默认值为0。你需要把
游戏级别
转换成
int
。哇,我觉得很傻。非常感谢您的快速回复!