나는 Fallout 3에서 발견 된 해킹 미니 게임을 시뮬레이트하는 프로그램을 작성하고 있습니다. 사용자가 난이도를 선택해야하고 프로그램에 따라 문자열 목록에 있으면 사용자가 프로그램에서 임의로 선택한 단어를 정확히 추측 할 수있는 4 가지 시도가 있습니다.Python - main() 함수에서 함수를 호출하면 0이 반환됩니다.
내가 직면 한 문제는 게임의 여러 측면을 처리하기 위해 작성한 다양한 기능을 호출 할 때 값이 반환되지 않는다는 것입니다. 별도로 각 함수를 호출하고 필요한 매개 변수를 전달하면 올바른 결과를 얻을 수 있지만 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을 컴파일러로 사용하고 있습니다. 감사.
나는 당신이 무엇을 요구하고 있는지 분명하지 않다고 생각합니다. 좀 더 정확히 무슨 일이 일어나는지 설명해 주시겠습니까? –
main에서'raw_input'은 문자열을 반환합니다. 이 문자열을 difficult_level에 전달 중입니다. 그래서 그것은 0 인 기본값을 반환합니다.'game_level'을'int'로 변환해야합니다. – eswarp25