def letterChoice():
playerLetter = input('Please choose X or O.').upper()
if playerLetter in ['X','O']:
print('The game will now begin.')
while playerLetter not in ['X','O']:
playerLetter = input('Choose X or O.').upper()
if playerLetter == 'X':
computerLetter = 'O'
else:
computerLetter = 'X'
turnChooser()
def turnChooser():
choice = input("Would you like to go first, second or decide by coin toss?(enter 1, 2 or c) ")
while choice not in ["1","2","c"]:
choice = input("Please enter 1, 2 or c. ")
if choice == 1:
print("G")
cur_turn = letterChoice.playerLetter()
elif choice == 2:
print("H")
else:
print("P")
moveTaker()
我不知道应该如何将playerLetter继承到turnChooser()中,我尝试将playerLetter放入每个函数的方括号中,但它们不会传递并创建参数错误,print("G")
等只是为了查看代码是否有效,但只要输入1或2,就会输出“p”。你知道吗
在包含变量类型的函数中:
显然,将variableName更改为实际调用的变量。希望这有帮助!你知道吗
汤米
将turnchooser()编辑为turnchooser(var),然后在调用函数时将字母传递给函数,如下所示:
def LetterChoice():
而且
字母将被放置在一个名为var的变量中,这意味着您的代码将使用该字母作为var而不是playerletter。你知道吗
当然你可以把名字改成任何你喜欢的名字。你知道吗
您可以向函数中添加尽可能多的变量,但是它们都应该分配一些内容,也就是说,您不能像这样调用上一个函数:
除非为其指定默认值:
这样,无论何时调用函数,“var”的值都是x,除非另有说明。你知道吗
请注意,如果要将它从一个函数传递到另一个函数,则必须将该字母赋给一个变量,然后在“LetterChoice”之外调用该函数,或者在“LetterChoice”的定义中调用该函数
您需要为
playerLatter
定义函数属性例如:
从其他函数访问
您可以随时检查给定函数的可用属性
相关问题 更多 >
编程相关推荐