为什么调用我的功能时不激活?

时间:2019-10-09 14:38:50

标签: python function

import random
def main():
   num_guesses = 4


   instruction_file=open('instructions.txt', 'r')

   list_of_words = ['apple', 'banana', 'watermelon', 'kiwi', 'pineapple', 'mango']
   answer=random.choice(list_of_words)
   puzzle=['_'] * len(answer)




   def display_instructions(instruction_file):
      file_contents=instruction_file.read()
      instruction_file=instruction_file.close()
      print(file_contents)





   def get_guess(num_guesses):
      print('The number of guesses remaining is ' + str(num_guesses)+ '.') 
      letter_input = input("Guess a letter ")
      return letter_input

   def update_puzzle_string(letter_input,puzzle,answer):
      if get_guess(num_guesses) in answer:
         for i,x in enumerate(answer):
            if x is get_guess:
               puzzle[i]=letter_input
               return True


   def display_puzzle_string(puzzle):
      print('The current state of the puzzle is '+str(puzzle))

   def is_word_found(puzzle,answer):
      is_word_found=True
      puzzle_string=print(''.join(puzzle))
      if puzzle_string == answer:
         return False





   def play_game(answer,puzzle):
      while True:
         display_puzzle_string(puzzle)
         get_guess(num_guesses)
         update_puzzle_string(get_guess,puzzle,answer)
         print(str(puzzle))



   is_word_found(puzzle,answer)
   display_instructions(instruction_file)         
   play_game(answer,puzzle)







main()

很抱歉格式化问题。该程序的目标是收集用户的猜测,然后将其与列表中随机选择的单词进行比较,从而更新一个谜题,该谜题的字母所在的空格为空白,如果单词的所有字母都被猜测为,则用户告诉他们是正确的。用户得到4个猜测。基本上是子手。当我执行该程序时,它只是打印指令,初始拼图状态,请求猜测然后不断询问猜测。我不明白为什么这行不通。在获得帮助之后,我将实现猜测的数量。

2 个答案:

答案 0 :(得分:0)

import random
def main():
    num_guesses = 4
    instruction_file=open('instructions.txt', 'r')

    list_of_words = ['apple', 'banana', 'watermelon', 'kiwi', 'pineapple', 'mango']
    answer=random.choice(list_of_words)
    puzzle=['_'] * len(answer)

    def display_instructions(instruction_file):
        file_contents=instruction_file.read()
        instruction_file=instruction_file.close()
        print(file_contents)

    def get_guess(num_guesses):
        print('The number of guesses remaining is ' + str(num_guesses)+ '.') 
        letter_input = input("Guess a letter: ")
        return letter_input

    def update_puzzle_string(letter_input,puzzle,answer):
        for i,x in enumerate(answer):
            if x is letter_input:
                puzzle[i]=letter_input
        return

    def display_puzzle_string(puzzle):
        print('The current state of the puzzle is '+str(puzzle))

    def is_word_found(puzzle,answer):
        is_word_found=True
        puzzle_string=print(''.join(puzzle))
        if puzzle_string == answer:
            return False

    def play_game(answer,puzzle):
        while True:
            display_puzzle_string(puzzle) #display '_ _ _ _ _'
            guess = get_guess(num_guesses)
            update_puzzle_string(guess,puzzle,answer)
            #print(str(puzzle)) #this statement is causing the repetitive puzzle prints

    is_word_found(puzzle,answer)
    display_instructions(instruction_file)
    play_game(answer,puzzle)

main()

在解决了格式问题之后,该代码现在反复询问您的猜测并接受输入。我认为该问题主要存在于格式设置中,因为现在没有错误。另外,您之前有随机进口吗?干杯

编辑:

查看对update_puzzle_string()play_game的更改。您反复调用了get_guess函数,而不是使用其初始返回值。 example (note that repeat letters are not supported under this code)

编辑2 :(请参阅此答案的注释)

有关“答案中同一字母的倍数”的更改,请参见update_puzzle_string() program now places all of the right letter in place

答案 1 :(得分:0)

所以您的错误是无法停止软件?

您可以在play_game中使用is_word_found,然后在找到后中断。

赞:

   id                                             fields
0   1  [{'field1': 1, 'field2': 'a'}, {'field1': 2, '...
1   2  [{'field1': 3, 'field2': 'c'}, {'field1': 4, '...
2   3                     [{'field1': 5, 'field2': 'e'}]

中断将停止def play_game(answer,puzzle): while True: display_puzzle_string(puzzle) get_guess(num_guesses) update_puzzle_string(get_guess,puzzle,answer) if is_word_found(puzzle, answer): print("You won!") break print(str(puzzle)) 无限循环