将IF语句与结果的最终脚本进行比较

时间:2016-02-18 19:42:43

标签: python if-statement

def coordinates():

    coordinate_X = int
    coordinate_Y = int

    userx = int(input("Please enter a number for coordinate x"))
    usery = int(input("coordinate y"))
    accepted = (True) or (False)
    accepted2 = (True) or (False)

    accepted = False
    if userx range(0, 600):
        return print("error")
    else accepted = True:
        return print("ook")

    accepted2 = False
    if usery range(0, 400):
    return print("error") 
        return print("error")
    else accepted2 = True:
        return print("ok")

    if accepted + accepted2 = True:
        return print("coordinates are ok")
    else:
        return print("coordinates are not ok")


coordinates()

我想要发生的是,如果前两个if语句是True(他们评估userxusery),那么底部的if语句将返回不同的答案。

3 个答案:

答案 0 :(得分:1)

def coordinates():
    userx = int(input("Please enter a number for coordinate x"))
    usery = int(input("coordinate y"))

    accepted = False
    if userx in range(0, 600):
        print("invalid x coordinate")
    else:
        accepted = True
        print("ok")

    accepted2 = False
    if usery in range(0, 400):
        print("invalid y coordinate")
    else:
        accepted2 = True
        print("ok")

    if accepted and accepted2:
        print("coordinates are ok")
    else:
        print("coordinates are not ok")

coordinates()

这样的东西?

答案 1 :(得分:1)

首先,由于布尔逻辑,accepted = (True) or (False)将简单地评估为accepted = True。相反,最初将accepted和accepted2初始化为= False

接下来,if accepted + accepted2 = True:,您遗漏了一个=个符号,=用于分配,==用于比较。

对于布尔逻辑,accepted + accepted2相当于accepted or accepted2,这意味着如果两者都是True,则结果将为True。如果您想检查两者都是True,请改用accepted and accepted2

所以你想要if accepted and accepted2 == True:作为你的if语句。

答案 2 :(得分:0)

试试这个:您应该使用=为变量赋值。如果要进行比较,请使用==。阅读this if-else语句familiartization。

def coordinates():

    userx = int(input("Please enter a number for coordinate x"))
    usery = int(input("coordinate y"))
    accepted = False
    accepted2 = False


    if userx in range(0, 600):
       print("invalid x coordinate")
    else:
      accepted = True
      print("ok")

    if usery in range(0, 400):
      print("invalid y coordinate")
    else:
      accepted2 = True
      print("ok")

    if accepted and accepted2:
      print("coordinates are ok")
    else:
      print("coordinates are not ok")


coordinates()