我怎样才能缩短这个python代码

时间:2015-12-29 14:00:20

标签: python loops

我想使用某种循环缩短此代码:for,if(if if),while,foreach或other。

def FindChannel():
    rc_program_up = "Ch+"
    rc_ok = "ok"
    rc_exit = "exit"
    value0 = checkPicture(15)

    if (value0 == 100):
        send_rckey(rc_exit)
    else:
        send_rckey(rc_program_up)
        value0 = checkPicture(15)

    if (value0 == 100):
        send_rckey(rc_exit)
    else:
        send_rckey(rc_program_up)
        value0 = checkPicture(15)

    if (value0 == 100):
        send_rckey(rc_exit)
    else:
        send_rckey(rc_program_up)
        value0 = checkPicture(15)

    if (value0 == 100):
        send_rckey(rc_exit)
    else:
        send_rckey(rc_program_up)
        value0 = checkPicture(15)

    if (value0 == 100):
        send_rckey(rc_exit)
    else:
        send_rckey(rc_program_up)
        value0 = checkPicture(15)

我做不到,请帮助我。

2 个答案:

答案 0 :(得分:3)

您可以使用for循环。 range(5)循环5次。 _表示您不想使用迭代器返回的值(在本例中为range(5)

for _ in range(5):
    if (value0 == 100):
        send_rckey(rc_exit)
    else:
        send_rckey(rc_program_up)
        value0 = checkPicture(15)

为了更好地理解for循环,请查看documentation(注意:xrange仅适用于Python 2,在Python 3中range相当于xrange })

答案 1 :(得分:0)

我喜欢在类中保留特定于使用的常量,比如

Entity user = new Entity("User");
Entity savingAccount=new Entity ("SavingAccount");
savingAccount.setProperty("UserID",  user.getKey());
相关问题