【问题标题】:Why won't my function activate when I call it?为什么我调用它时我的函数不会激活?
【发布时间】:2019-10-09 14:38:50
【问题描述】:
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 次猜测。基本上是刽子手。当我执行这个程序时,它只打印指令、初始拼图状态、请求猜测然后继续要求猜测。我不明白为什么这不起作用。在获得帮助后,我将实施猜测次数。

【问题讨论】:

  • 你的代码有严重的缩进问题,这对于 Python 来说真的很难理解。你能纠正吗?只需编辑您的问题,粘贴您的代码,选择它,然后使用{} 按钮将其格式化为代码。不要对大代码块使用反引号。
  • @FredLarson 完成,谢谢!
  • 当我运行此代码时(在顶部添加import random 后),它一直要求输入。
  • @JohnGordon 那是我的问题,我无法停止要求猜测。
  • 在编程时,您应该采取更“循序渐进”的方法,您的代码中似乎有很多基本函数没有按照您的预期执行。此外,您将某些函数与变量混淆,并且您没有使用函数返回的内容。

标签: python function


【解决方案1】:
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 函数而不是使用它的初始返回值。

编辑2:(参考这个答案的cmets)

请参阅update_puzzle_string() 以了解有关“答案中有多个相同字母”的更改

【讨论】:

  • 哦,对不起,我的意思是它一直要求猜测,而不是用猜测更新拼图字符串。就像它只是一遍又一遍地询问猜测,而不是继续更新拼图或打印更新的拼图。
  • 非常感谢!我迷路了,难以置信,你救了我一命。
  • @Mathissohardlmao 没问题,很高兴我能帮上忙!感谢您接受答案:)
  • 与原始问题无关,但为什么猜测没有填写单词的所有适当字母。例如。西瓜的猜测 e 只填入其中一个。
  • @Mathissohardlmao 查看我对代码更改的更新答案。
【解决方案2】:

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

您可以在 play_game 中使用 is_word_found ,如果找到则中断。

像这样:

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))

Break 将停止while True 无限循环

【讨论】:

  • 他最初的问题是重复调用函数创建的循环,而不是将函数的结果保存到变量中并使用它。
猜你喜欢
  • 1970-01-01
  • 2020-10-19
  • 2018-11-02
  • 2018-08-12
  • 2011-06-24
  • 2023-03-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多