即使条件为真,if语句也没有运行

时间:2015-03-23 09:47:56

标签: python if-statement

即使条件为真,if条件也没有运行。在第29行有一个函数set_row,在第31行有一个函数set_col。在第58行,有一个if语句,用于检查guess_row1是否等于set_row和guess_col1 = set_col,即使条件为真,如果语句不运行,则始终运行

from random import randint
user1 = {
    "name":input("Enter your name"),
    "board":[]
}
user2 = {
    "name":input("Enter your name"),
    "board":[]
}

def set_board(user):
    for x in range(5):
        user["board"].append(["O"]*5)

def print_board(user):
    for row in user["board"]:
        print(" ".join(row))
print("")
print("Let's start")
print("")
set_board(user1)
set_board(user2)
print("This is {}'s board".format(user1["name"]))
print_board(user1)
print("")
print("This is {}'s board".format(user2["name"]))
print_board(user2)

def set_row(user1):
    return randint(0, len(user1["board"])-1)
def set_col(user1):
    return randint(0, len(user1["board"])-1)

print("")
print("Answer for {}".format(user1["name"]))
print(set_row(user1))
print(set_col(user1))
print("")
def set_row2(user2):
    return randint(0, len(user2["board"])-1)
def set_col2(user2):
    return  randint(0, len(user2["board"])-1)
print("Answer for {}".format(user2["name"]))

print(set_row2(user2))
print(set_col2(user2))


print("")
for turnu1 in range(3,0,-1):
    print("turn's left for {} ".format(user1["name"]) + str(turnu1))
    print("")
    guess_row1 = int(input("Enter the row "))
    guess_col1 = int(input("Enter the col "))
    print("")
    if guess_row1==set_row(user1) and guess_col1==set_col(user1):
        print("your right")
    else:
        print("Wrong answer")

1 个答案:

答案 0 :(得分:1)

当您拨打并打印用户2的随机数时:

print(set_row2(user2))
print(set_col2(user2))

然后这些数字由randint()生成,并且从不存储在任何地方。

用户1的呼叫将是随机数:

if guess_row1==set_row(user1) and guess_col1==set_col(user1):

并且与第一组调用不对应。如果你的意思相同,那么存储你首先选择的随机值:

user2_row = set_row2(user2)
user2_col = set_col2(user2)

所以你可以稍后对它们进行测试:

if guess_row1 == user2_row and guess_col1 == user2_col: