【问题标题】:Python defining functions in Tic Tac ToePython 在井字游戏中定义函数
【发布时间】:2020-04-11 07:33:18
【问题描述】:

我正处于用 python 编写一个简单的井字游戏的早期阶段。我已经定义了 3 个函数,一个是初始化游戏,一个是绘制棋盘,一个是询问玩家是 X 还是 O。据我所知,我觉得我的函数是按顺序和正确的顺序请求的,但我无法让程序移过第一个输入部分。任何帮助都会很棒。

def start():
    print("Do you want to play Tic Tac Toe? Type yes or no.")
    choice = input()
    while choice == ('yes','no'):
        if choice.lower == ('yes'):
           player_choice()
        elif choice.lower == ('no'):
           print("goodbye")
           quit()
        else:
              print("That was not a valid response. Type yes or no.")
start()

def drawsmall():
    a = (' ___' *  3 )
    b = '   '.join('||||')
    print('\n'.join((a, b, a, b, a, b, a, )))



def player_choice():
    print("Player one it's time to choose, X or O")
    select= input()
    if select.lower ==("x","o"):
        print("Let the game begin.")
        drawsmall()
    elif select.lower != ('x','o'):
        print("Please choose either X or O.")
    else:
        print("Come back when you're done messing around.")
        quit()

【问题讨论】:

  • 你能用文字解释一下你期望看到的事件的顺序吗?
  • 是的,我很抱歉。基本上,在我看来,流程是这样的。 def start(),初始化游戏并要求玩家输入对应于是或否的输入。如果是,则应该将其传递给 player_choice,它会询问玩家一他们想要成为 X 还是 O。玩家选择应该通过,玩家一选择 X 还是 O 来绘制小,它应该绘制实际的棋盘。
  • 我认为第一个问题(没有所有代码很难知道完整的流程)在这里:'if select.lower ==("x","o")',应该是'if select.lower in ("x", "o")';和你的 elif 一样,它应该只是 'else:' (选择可能等于或不等于“x”或“o”。它不能是任何其他东西)。
  • 抱歉,之前没看到。这里也是一样的:while 选择 == ('yes','no')。应该是'while selection not in ('yes', 'no'):'

标签: python tic-tac-toe


【解决方案1】:

嗯,在弄清楚你的意图之后,我看到了一些必须改变的事情。

首先,试试这个看看:

def start():
  print("Do you want to play Tic Tac Toe? Type yes or no.")
  while True:
    choice = input()
    if choice.lower() == 'yes':
      player_choice()
    elif choice.lower() == 'no':
      print("goodbye")
      quit()
    else:
      print("That was not a valid response. Type yes or no.")


def drawsmall():
  a = (' ___' * 3)
  b = '   '.join('||||')
  print('\n'.join((a, b, a, b, a, b, a,)))


def player_choice():
  print("Player one it's time to choose, X or O")
  select = input()
  if select.lower() in ("x", "o"):
    print("Let the game begin.")
    drawsmall()
  else:
    print("Please choose either X or O.")

start()
  1. .lower,应该是.lower()
  2. start() 必须在最后,而不是在程序中间(否则它不会找到像 player_choice() 这样的其他函数
  3. 字符串不是('string'),你必须去掉括号
  4. 如果你想比较一个变量和几个值,你必须使用 in,而不是 ==
  5. 使用标准输出时,玩家选择的“X”和“O”将很难绘制!!!

【讨论】:

  • 非常感谢。我对这一切都很陌生,并且一直在努力自学。很抱歉我之前的描述不清楚。
  • 别担心!我们都在学习。试着解释你的问题,你尝试了什么,你的输入和输出,以便人们可以更好地帮助你......如果你犯了错误,不要犹豫,继续尝试!很高兴能提供帮助。
【解决方案2】:

首先,您的问题是您错误地调用了lower 方法。你应该这样称呼它:

str = 'Test'
print(str.lower())
print(str.lower)
>> test
>> <built-in method lower of str object at 0x7ff42c83ebb0>

解决此问题,您将进入正确的条件

其次,您应该更改 start() 中的 while 循环,如下所示:

def start():
    print("Do you want to play Tic Tac Toe? Type yes or no.")
    choice = ‘’
    while choice not in ['yes','no']:
        choice = input()
        if choice.lower() == 'yes':
           player_choice()
        elif choice.lower() == 'no':
           print("goodbye")
           quit()
        else:
              print("That was not a valid response. Type yes or no.")

请注意,如果您不设置choice = '',您将不会进入循环。

第三,你应该将调用start()函数移动到所有函数减速的末尾,以便它们都能被正确识别。

注意

用我在start() 中为您提供的逻辑更正player_choice()

【讨论】:

    猜你喜欢
    • 2020-08-13
    • 2014-04-12
    • 1970-01-01
    • 1970-01-01
    • 2017-09-18
    • 2013-12-21
    • 2019-11-24
    • 2017-02-16
    相关资源
    最近更新 更多