Python函数有多个IF语句?

时间:2018-09-01 03:42:56

标签: python for-loop if-statement

我对循环和函数有疑问,因此有一个值列表,这些值作为三个输入变量,例如0-5或方向(西,北等),我不确定是否正确使用了If语句。我在for循环命令或函数中做错了什么吗?

数组输入变量的示例,但数据集中没有任何设置限制:

def follow_path(a):
    legend()    #draw legend

    for draw in a:
        direction(a[0])
        location(a[1])
        choosetoken(a[2])


def location(corner):
    if corner == 'Top left':
        goto(0,600)
    if corner == 'Top right':
        goto(600,600)
    if corner == 'Bottom left':
        home()
    if corner == 'Bottom right':
        goto(600,0)
    if corner == 'Center':
        goto(300,300)
    if corner == 1:
        forward(100)
    if corner == 2:
        forward(200)
    if corner == 3:
        forward(300)
    if corner == 4:
        forward(400)
    if corner == 5:
        forward(500)
    else:
        print ("Check input '1' is correct or not")

def direction(direction):
    if direction == 'West':
        setheading(180)
    if direction == 'East':
        setheading(0)
    if direction == 'North':
        setheading(90)
    if direction == 'West':
        setheading(270)
    if direction == 'Start':
        home()   
    else:
        print ("Check input '0' is correct or not")

def choosetoken(a): #Draw shapes
    if a == 0:
        youtube()
    elif a == 1:
        chrome()
    elif a == 2:
        googledrive()
    elif a == 3:
        gmail()
    elif a == 4:
        photo()
    else:
        print ("Token Value out of range, check if input '2' is correct or not")

如果我运行该函数,它将始终为我提供其他函数,并且不会遵循任何命令。

enter image description here

using (NamedPipeServerStream namedPipeServer = new NamedPipeServerStream("test-pipe", PipeDirection.InOut, 1, PipeTransmissionMode.Message))
{
    byte[] bytes = Encoding.Default.GetBytes("Hello, it's me!\n");
    namedPipeServer.WaitForConnection();
    Console.WriteLine("A client has connected!");
    namedPipeServer.Write(bytes, 0, bytes.Length);
}

1 个答案:

答案 0 :(得分:1)

首先,if语句遵循的顺序是:

if condition:
    doSomething()
elif anotherCondition:
    doSomethingDifferent()
elif anotherAnotherCondition:
    doSomethingDifferentAgain()
else:    #otherwise - if the above conditions don't satisfy(are not True)
    doThis()

第二; for循环存在一个问题,您需要将列表a传递到follow_path()中,然后将列表的第一,第二和第三个元素传递到方向,位置和选择令牌中。

def follow_path(a):
    for draw in a:
        direction(a[0])
        location(a[1])
        choosetoken(a[2])

def direction(thing):
    print("direction " + str(thing))

def location(thing):
    print("location " + str(thing))

def choosetoken(thing):
    print("choosetoken " + str(thing))

a = [['Start', 'Bottom right', 1],['South', 1, 1], ['North', 3, 4], ['West', 4, 0], ['West', 2, 0], ['South', 3, 4]]
follow_path(a)

这是故意的吗?还是你想要这样的东西?

def follow_path(a):
    for draw in a:
        direction(draw[0])
        location(draw[1])
        choosetoken(draw[2])

def direction(thing):
    print("direction " + str(thing))

def location(thing):
    print("location " + str(thing))

def choosetoken(thing):
    print("choosetoken " + str(thing))

a = [['Start', 'Bottom right', 1],['South', 1, 1], ['North', 3, 4], ['West', 4, 0], ['West', 2, 0], ['South', 3, 4]]
follow_path(a)

正在发生的事情是您正在遍历列表a;并从每次迭代中选择第零,第一和第二项。

因此,第一个迭代将是['Start','Bottom right',1],我选择第零个,第一个和第二个; “开始”,“右下”,1,然后移至下一个迭代,分别为['South',1、1],执行相同的操作,依此类推。

我希望这会有所帮助:)