跳至主要內容

Chess Problem v4

AI悦创原创2023年3月18日墨尔本大学CSPython一对一辅导unimelb墨尔本大学CSPython一对一辅导unimelb大约 4 分钟...约 1164 字

Chess Problem v4

象棋问题v4

OK, final stop in chess land, extending check_move() again. Previously, your function took two separate arguments: a column and a row value. Now you will rewrite the function to accept the board position as a single str argument. In other words, the input to check_move(position) will now be a single position string such as 'B5', that encodes both the column and the row designator.

好的,象棋世界的最后一站,再次扩展' check_move() '。以前,函数有两个单独的参数:一个列值和一个行值。现在您将重写函数,以接受董事会位置作为单个' str '参数。换句话说,' check_move(position) '的输入现在将是一个单一的位置字符串,例如" B5' ',它对列指示符和行指示符进行了编码。

Note that, irrespective of the casing of the column value, your code should output the value in upper case. Also note that there is no guarantee that the input is made up of exactly two characters.

请注意,无论列值的大小写如何,代码都应该以大写输出值。还要注意,不能保证输入恰好由两个字符组成。

Your function should work like this:

你的函数应该像这样工作:

>>> check_move('B4')
'The piece is moved to B4.'
>>> check_move('b4')
'The piece is moved to B4.'

and like this with an invalid input:

像这样输入无效:

>>> check_move('I4')
'The column value is not in the range a-h or A-H!'
>>> check_move('F9')
'The row value is not in the range 1 to 8!'
>>> check_move('A')
'The position is not valid.'

Answer

def check_move(position):
    if len(position) > 2 or len(position) < 2:
        return 'The position is not valid.'
    elif position[0].lower() in ['a','b','c','d','e','f','g','h'] and 1 <= int(position[1]) <= 8:
        return f'The piece is moved to {position[0].upper()}{position[1]}.'
    elif position[0].lower() not in ['a','b','c','d','e','f','g','h'] and 1 <= int(position[1]) <= 8:
        return 'The column value is not in the range a-h or A-H!'
    elif position[0].lower() in ['a','b','c','d','e','f','g','h'] and int(position[1]) not in (1,8):
        return 'The row value is not in the range 1 to 8!'
公众号:AI悦创【二维码】

AI悦创·编程一对一

AI悦创·推出辅导班啦,包括「Python 语言辅导班、C++ 辅导班、java 辅导班、算法/数据结构辅导班、少儿编程、pygame 游戏开发、Web、Linux」,全部都是一对一教学:一对一辅导 + 一对一答疑 + 布置作业 + 项目实践等。当然,还有线下线上摄影课程、Photoshop、Premiere 一对一教学、QQ、微信在线,随时响应!微信:Jiabcdefh

C++ 信息奥赛题解,长期更新!长期招收一对一中小学信息奥赛集训,莆田、厦门地区有机会线下上门,其他地区线上。微信:Jiabcdefh

方法一:QQ

方法二:微信:Jiabcdefh

通知
关于编程私教&加密文章