Python 在 Tic Tac Toe 中定义函数
Python defining functions in Tic Tac Toe
我正处于 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()
首先,你的问题是你调用的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()
嗯,在弄清楚你的意图之后,我看到了一些必须改变的东西。
首先,试试看:
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()
- .lower,应该是.lower()
- start() 必须走在最后,不能在程序中间(否则会找不到像player_choice()
这样的其他函数)
- 一个字符串不是('string'),你要去掉括号
- 如果你想比较一个变量和多个值,你必须使用in,而不是==
- 玩家在使用标准输出时选择的'X'和'O'会很难画出来!!!
我正处于 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()
首先,你的问题是你调用的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()
嗯,在弄清楚你的意图之后,我看到了一些必须改变的东西。
首先,试试看:
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()
- .lower,应该是.lower()
- start() 必须走在最后,不能在程序中间(否则会找不到像player_choice() 这样的其他函数)
- 一个字符串不是('string'),你要去掉括号
- 如果你想比较一个变量和多个值,你必须使用in,而不是==
- 玩家在使用标准输出时选择的'X'和'O'会很难画出来!!!