简体   繁体   English

在Python中组合布尔运算符的正确方法(语法)是什么

[英]What is the correct way (syntax) of combining boolean operators in python

So i'm working on a simple python game (Merels). 所以我正在研究一个简单的python游戏(Merels)。 this function is supposed to let the player enter what enemy token he would like to remove but asking him again if he enters an invalid position. 这个功能应该让玩家输入他想要移除的敌人标记,但是再次询问他是否输入了无效位置。 the "layer" variable stands for the square the position is in (a board of merels is like 3 concentric squares) and the "layer" variable stands for the position in the layer. “ layer”变量代表位置所在的正方形(梅雷尔板像3个同心正方形),而“ layer”变量代表位置所在的正方形。 there should be no logical flaws. 应该没有逻辑上的缺陷。 on the third last line an error message pops up and that says: 'TypeError: list indices must be integers, not unicode'. 最后三行弹出一条错误消息,提示:“ TypeError:列表索引必须是整数,而不是unicode”。

def playerRemove(board, removeLetter):
    print("You have scored a mill! Choose an enemy Token to remove.")
    layer=0
    print("First enter the layer. (1-3)")
    layer=raw_input()
    while layerEmpty(board, layer, removeLetter):
        print("First enter the layer. (1-3)")
        layer=raw_input()

    position=9
    layer=int(layer)
    position=int(position)
    while board[layer][position]!=removeLetter or isMill(board, computerLetter):
        print("Now enter the position. (1-8)")
        position=raw_input()

i've had this problem many times but so far the solution was to convert the variable into an integer with an expression like 我曾多次遇到此问题,但到目前为止,解决方案是将变量转换为具有如下表达式的整数

x=int(x)

but somehow this isnt working here. 但不知何故这在这里不起作用。

Before the loop you're correctly doing position = int(position) , so you successfully enter into the loop, but then within the loop you ask for a new position , at the line: 在循环之前,您正确地执行position = int(position) ,因此您成功进入了循环,但是在循环中,您在以下position要求一个新的position

position = raw_input()

Change this to: 更改为:

position = int(raw_input())

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM