Python Forum
Python IF statements ignored
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Python IF statements ignored
#1
Hello forum

for my university I need to create a simple poker hand evaluator. I have a code so far and all but 3 things work.
I will post the code underneath.

for some reason the 4 of a kind, straight flush and royal flush do not work. even though my straight and flush bools are true, the code which adds

if straight true and flush true: straight flush = true

will be ignored. and thus, royal flush is ignored.

and for a reason I dont know, it will not consider 4 of a kind at all.

I*d be happy for some help because in c++ this similar code structure worked for me :)


The basic codes works as follows: You input a card and its symbol with each one input. cards go from 2-14 (2 to ace) and colors from 1-4 (dont matter the symbol). And it will sort (zip) the hand in ascending order and evaluate its value with all the if statements underneath. All but the 3 mentioned do work. Straightflush and therefore royalFlush do not work, 4 of a kind does not work either.

I'd be happy if some1 could help me.

Thanks in advance!

# farben: pick, herz, kreuz, karo : 1,2,3,4
# werte: 2-ass(14): 2,3,4,5,6,7,8,9,10,11,12,13,14

print("*********")
print("P O K E R")
print("*********")
print("EINGABE IHRER KARTEN")
print("Geben Sie die höchste Karte ein: ")
print()
wert1 = int(input("1. Wert: "))
farbe1 = int(input("1. Farbe: "))
wert2 = int(input("2. Wert: "))
farbe2 = int(input("2. Farbe: "))
wert3 = int(input("3. Wert: "))
farbe3 = int(input("3. Farbe: "))
wert4 = int(input("4. Wert: "))
farbe4 = int(input("4. Farbe: "))
wert5 = int(input("5. Wert: "))
farbe5 = int(input("5. Farbe: "))
print()
print("Sie haben eingegeben: ")
print("Karte 1 (Wert|Farbe): ", wert1, farbe1)
print("Karte 2 (Wert|Farbe): ", wert2, farbe2)
print("Karte 3 (Wert|Farbe): ", wert3, farbe3)
print("Karte 4 (Wert|Farbe): ", wert4, farbe4)
print("Karte 5 (Wert|Farbe): ", wert5, farbe5)


onepair = False #wert i = wert j mit allen Karten durchiterieren
twopair = False #onepair==true plus wert k = wert l mit allen karten ausser onepair
threeOfKind = False #onepair == true and wert m == wert n or wert i == wert j == wert k
straight = False #werti+1 == wertj, wertj+1 == wert k ... (5 mal)
flush = False # farbe1 == farbe2 == farbe3 == farbe 4 == farbe 5 and straight == False
FullHouse = False #three of a kind == True and onepair == True
fourOfKind = False #wert1==wert2==wert3==wert4 or wert2=wert3=wert4=wert5 or wert1=wert2=wert3=wert5 or ...
straightFlush = False #straight == True and flush == True
royalFlush = False #highestcard == ass and straight == True and flush == True

#List approach:
werte = (wert1, wert2, wert3, wert4, wert5)
farben = (farbe1, farbe2, farbe3, farbe4, farbe5)
#zip referenced sort, damit werte in aufsteigender reihenfolge sind, werte(5) ist in jedem fall die hoechste karte
#und alle farben sind immernoch zugeordnet:
werte, farben = zip(*sorted(zip(werte, farben))) #klappt. Ergebnis: Karten in aufsteigender Reihenfolge (werte und farben zugehoerig)

#referenz in Liste: farben[0], farben[1]... etc.

#Bewertung der Hand


#1. one pair
if werte[0] == werte[1] or werte[2] == werte[1] or werte[2] == werte[3] or werte[3] == werte[4]:
    onepair = True

#2. Two pair (one pair plus second pair)
if onepair == True:
    if werte[0] == werte[1]:
        if werte[2] == werte[3] or werte[3] == werte[4]:
            twopair = True
            onepair = False
    if werte[1] == werte[2]:
        if werte[3] == werte[4]:
            twopair = True
            onepair = False

#3. Triplet (three of kind)
if werte[0] == werte[1] == werte[2] or werte[3] == werte[1] == werte[2] or werte[3] == werte[4] == werte[2]:
    threeOfKind = True
    onepair = False

#4. Straight: (ascending werte)
if werte[4] == werte[3]+1 == werte[2]+2 == werte[1]+3 == werte[0]+4:
    straight = True
    onepair = False
    twopair = False
    threeOfKind = False

#5. flush
if farben[0] == farben[1] == farben[2] == farben[3] == farben[4]:
    flush = True
    onepair= False
    twopair = False
    threeOfKind = False

#6. full house; triplet+onepair
if werte[0] == werte[1] == werte[2] and werte[2]!= werte[3] and werte[3] == werte[4]:
        FullHouse = True
        threeOfKind = False
        onepair = False
        twopair = False
        straight = False
        flush = False

if werte[2] == werte[3] == werte[4] and werte[2]!= werte[1] and werte[1] == werte[0]:
        FullHouse = True
        threeOfKind = False
        onepair = False
        twopair = False
        straight = False
        flush = False

#7. 4 of a kind: weil ich meine Karten der groesse nach geordnet habe, kann die 5. karte nur auf pos 1 oder 5 sein
if werte[0] == werte[1] == werte[2] == werte[3] or werte[4] == werte[1] == werte[2] == werte[3]:
    fourOfKind == True
    onepair = False
    twopair = False
    threeOfKind = False
    FullHouse = False
    straight = False
    flush = False

#8. Straight flush
if flush == True and straight == True:
    straightFlush == True
    straight == False
    flush == False
#9. Royal Flush
if straightFlush == True and werte[4] == 14:
    royalFlush == True
    straightFlush == False


#Ergebnis printen:
if onepair == True: print("You have One Pair.")
if twopair == True: print ("You have Two Pairs")
if threeOfKind == True: print ("You have Three of a Kind")
if straight == True: print ("You have a Straight")
if flush == True: print ("You have a Flush")
if FullHouse == True: print ("You have a Full House")
if fourOfKind == True: print ("You have Four of a Kind")
if straightFlush == True: print ("You have a straight Flush")
if royalFlush == True: print ("You have a royal Flush")
Reply
#2
On lines 104 and 114 through 120, you're doing evaluation instead of assignment (lines 3 and 13 through 19 below):

#7. 4 of a kind: weil ich meine Karten der groesse nach geordnet habe, kann die 5. karte nur auf pos 1 oder 5 sein
if werte[0] == werte[1] == werte[2] == werte[3] or werte[4] == werte[1] == werte[2] == werte[3]:
    fourOfKind == True
    onepair = False
    twopair = False
    threeOfKind = False
    FullHouse = False
    straight = False
    flush = False
 
#8. Straight flush
if flush == True and straight == True:
    straightFlush == True
    straight == False
    flush == False
#9. Royal Flush
if straightFlush == True and werte[4] == 14:
    royalFlush == True
    straightFlush == False
Once that's corrected, your code should work.
Reply
#3
Ohala merci beaucoup mec.

to many ==== messed me up.

have a good day! Dance
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020