我想简化if语句,而不是键入fifth_turn == each_turn_before
table()
fifth_turn = int(input('Player #1, select the spot you desire: '))
if fifth_turn == first_turn or fifth_turn == second_turn or fifth_turn == third_turn or fifth_turn == fourth_turn:
print('Spot Taken')
elif fifth_turn == 1:
spot1 = 'x'
elif fifth_turn == 2:
spot2 = 'x'
elif fifth_turn == 3:
spot3 = 'x'
elif fifth_turn == 4:
spot4 = 'x'
elif fifth_turn == 5:
spot5 = 'x'
elif fifth_turn == 6:
spot6 = 'x'
elif fifth_turn == 7:
spot7 = 'x'
elif fifth_turn == 8:
spot8 = 'x'
elif fifth_turn == 9:
spot9 = 'x'
else:
print('ERROR')
如果我没有误解你的意图,我认为使用列表可以更有效地完成。你知道吗
通过将点组织到一个列表中并使用
in
操作符,代码中有很多地方可以改进:顺便说一句,打印“错误”是非常缺乏信息的,因为它没有告诉用户实际发生了什么以及如何修复它。你知道吗
你也应该考虑有一个轮次列表,而不是五个(或更多?)转向变量:
如果你正试图编程tic-tac-toe,其他的优化是可能的(比如根本没有转弯列表)。你知道吗
相关问题 更多 >
编程相关推荐