python - 为什么我的函数在调用时不会激活?

标签 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 个猜测。基本上就是刽子手。当我执行这个程序时,它只是打印指令、初始拼图状态、请求猜测,然后不断要求猜测。我不明白为什么这不起作用。在获得帮助后,我将实现猜测的数量。

最佳答案

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

关于python - 为什么我的函数在调用时不会激活?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58306650/

相关文章:

python - 当多个元素具有最高计数时,pandas describe() - top 如何工作?

c - 程序总是输出0值

javascript - 在函数中传递对象

c++ - 如何使用具有不同参数的函数作为函数参数

c++ - 函数返回看似随机的数字,而不是返回变量的值

c++ - VB.NET DLL中的C++ DLL函数复制

php - MySQL RENAME TABLE 语句如何工作/执行?

Python 3 在系统调用中转义引号

python - 保存在 pyplot 中

python - 在 Python 2.7 中计算和存储函数输出?