井字游戏中的Python定义功能

问题描述 投票:0回答:1

我正处于用python编写简单的Tic Tac Toe游戏的初期。我定义了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()
python tic-tac-toe
1个回答
0
投票

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

str = 'Test'
print(str.lower())
>> test

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

第二秒钟,您应该像下面这样更改while中的start()循环:

def start():
    print("Do you want to play Tic Tac Toe? Type yes or no.")
    choice = input()
    while choice in ['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()函数的调用移到所有函数减速的末尾,以使它们都被正确识别


0
投票

输入后出现错误吗?

© www.soinside.com 2019 - 2024. All rights reserved.