Abfragen kürzen

Wenn du dir nicht sicher bist, in welchem der anderen Foren du die Frage stellen sollst, dann bist du hier im Forum für allgemeine Fragen sicher richtig.
Antworten
Benutzeravatar
sorgenlos
User
Beiträge: 69
Registriert: Donnerstag 15. Februar 2007, 00:52

Kann man so eine Abfrage verkürzen?

Code: Alles auswählen

    if keingabe == (0,0) or keingabe == (0,1) or keingabe == (0,2) or keingabe == (0,3) or keingabe == (0,4) or keingabe == (1,0) or keingabe == (1,1) or keingabe == (1,2) or keingabe == (1,3) or keingabe == (1,4) or keingabe == (2,0) or keingabe == (2,1) or keingabe == (2,2) or keingabe == (2,3) or keingabe == (2,4) or keingabe == (3,0) or keingabe == (3,1) or keingabe == (3,2) or keingabe == (3,3) or keingabe == (3,4) or keingabe == (4,0) or keingabe == (4,1) or keingabe == (4,2) or keingabe == (4,3) or keingabe == (4,4):
        l1.append(Eingabe)
BlackJack

Ja. (Wenn ich jetzt nichts übersehen habe)

Code: Alles auswählen

if 0 <= keingabe[0] <= 4 and 0 <= keingabe[1] <= 4:
    ll.append(eingabe)

# oder

if all(0 <= n <= 4 for n in keingabe):
    ll.append(eingabe)
Benutzeravatar
sorgenlos
User
Beiträge: 69
Registriert: Donnerstag 15. Februar 2007, 00:52

vielen danke

die erste methode hat auf anhieb funktioniert!!! :D
Benutzeravatar
sorgenlos
User
Beiträge: 69
Registriert: Donnerstag 15. Februar 2007, 00:52

wie kann ich bspw. solche if-abfragen (schematisch betrachtet) kürzen?

Code: Alles auswählen

    if (x-10, y+20) in sb and (x-10, y+20) not in list:
        new = solve(sb[:], (x-10, y+20))
        jump((x-10, y+20), new)
        list.append(a_p)

    if (x-10, y-20) in sb and (x-10, y-20) not in list:
        new = solve(sb[:], (x-10, y-20))
        jump((x-10, y-20), new)
        list.append(a_p)
        
    if (x-20, y+10) in sb and (x-20, y+10) not in list:
        new = solve(sb[:], (x-20, y+10))
        jump((x-20, y+10), new)
        list.append(a_p)

    if (x-20, y-10) in sb and (x-20, y-10) not in list:
        new = solve(sb[:], (x-20, y-10))
        jump((x-20, y-10), new)
        list.append(a_p)

    if (x+10, y-20) in sb and (x+10, y-20) not in list:
        new = solve(sb[:], (x+10, y-20))
        jump((x+10, y-20), new)
        list.append(a_p)

    if (x+10, y+20) in sb and (x+10, y+20) not in list:
        new = solve(sb[:], (x+10, y+20))
        jump((x+10, y+20), new)
        list.append(a_p)

    if (x+20, y+10) in sb and (x+20, y+10) not in list:
        new = solve(sb[:], (x+20, y+10))
        jump((x+20, y+10), new)
        list.append(a_p)

    if (x+20, y-10) in sb and (x+20, y-10) not in list:
        new = solve(sb[:], (x+20, y-10))
        jump((x+20, y-10), new)
        list.append(a_p)
EyDu
User
Beiträge: 4881
Registriert: Donnerstag 20. Juli 2006, 23:06
Wohnort: Berlin

Code: Alles auswählen

eggs = (-20, -10, 10, 20)
for xoff in eggs:
    for yoff in eggs:
        spam = (x+xoff, y+yoff)
        
        if spam in sb and spam not in list:
            new = solve(sb[:], spam)
            jump(spam, new)
            list.append(a_p)
"list" als Name ist übrigens keine kluge Idee, damit verdeckst du den Builtin-Typ "list".

Edit: Ach, eine Klammer zu viel.
Edit2: "Edit" falsch geschieben :D
Antworten