import random
playerIn = True
dealerIn = True
#deck/player hand
deck = [2, 3, 4, 5, 6, 7, 8, 9, 10, 2, 3, 4, 5, 6, 7, 8, 9, 10, 2, 3, 4, 5, 6, 7, 8, 9, 10, 2, 3, 4, 5, 6, 7, 8, 9, 10,
'J', 'Q', 'K', 'A', 'J', 'Q', 'K', 'A', 'J', 'Q', 'K', 'A', 'J', 'Q', 'K', 'A']
playerHand = []
dealerHand = []
#deal cards
def dealCard(turn):
card = random.choice(deck)
turn.append(card)
turn.remove(card)
#calculate hand total
def total(turn):
total = 0
face = ['Q', 'K', 'J']
for card in turn:
if card in range(1, 11):
total += card
elif card in face:
total += 10
else:
if total > 11:
total += 1
else:
total += 11
return total
#check for winner
def revealDealerHand():
if len(dealerHand) == 2:
return dealerHand(0)
elif len(dealerHand) > 2:
return dealerHand(0), dealerHand(1)
#game loop
for _ in range(2):
dealCard(dealerHand)
dealCard(playerHand)
while playerIn or dealerIn:
print(f"The dealer had {revealDealerHand} and X")
print(f"You have {playerHand} for a total of {total(playerHand)}")
if playerIn:
stayOrHit = input("1: stay\n2: hit\n")
if total(dealerHand) > 16:
dealerIn = False
else:
dealCard(dealerHand)
if stayOrHit == '1':
playerIn == False
else:
dealCard(playerHand)
if total(playerHand) >= 21:
break
elif total(dealerHand) >= 21:
break
if total(playerHand) == 21:
print(f"\nYou have {playerHand} for a total of {playerHand} and the dealer has {dealerHand} for a total of {total(dealerHand)}")
print("Blackjack! You win!")
elif total(dealerHand) == 21:
print(f"\nYou have {playerHand} for a total of {playerHand} and the dealer has {dealerHand} for a total of {total(dealerHand)}")
print("Blackjack! Dealer wins!")
elif total(playerHand) >21:
print(f"\nYou have {playerHand} for a total of {playerHand} and the dealer has {dealerHand} for a total of {total(dealerHand)}")
print("You bust. Dealer wins.")
elif total(dealerHand) > 21:
print(f"\nYou have {playerHand} for a total of {playerHand} and the dealer has {dealerHand} for a total of {total(dealerHand)}")
print("Dealer busts. You win.")
elif 21 - total(dealerHand) < 21 - total(playerHand):
print(f"\nYou have {playerHand} for a total of {playerHand} and the dealer has {dealerHand} for a total of {total(dealerHand)}")
print("Dealer wins.")
elif 21 - total(dealerHand) > 21 - total(playerHand):
print(f"\nYou have {playerHand} for a total of {playerHand} and the dealer has {dealerHand} for a total of {total(dealerHand)}")
print("You win.")
Wenn das Programm Zeile 47 erreicht, erhalte ich statt zwei Kartenwerte und eine Gesamtsumme:
Der Dealer hatte und X
Sie haben [] für eine Gesamtsumme von 0
Ich habe lange versucht herauszufinden, wie ich die Hände des Spielers und des Dealers ausdrucken und die Gesamtsumme des Spielers ausdrucken kann Aber ich kann es für mein Leben nicht herausfinden. Irgendwelche Vorschläge?
Ich dachte, ich müsste die Werte der Hände des Spielers und des Dealers in einen Int-Wert umwandeln, aber das funktioniert nicht. Ich übersehe wahrscheinlich etwas wirklich Offensichtliches, aber ich habe ehrlich gesagt keine Ahnung.
Als Hintergrund habe ich erst vor ein paar Tagen angefangen und folge mit einem Blackjack-Tutorial. Hier ist der Code: [code]import random playerIn = True dealerIn = True
#calculate hand total def total(turn): total = 0 face = ['Q', 'K', 'J'] for card in turn: if card in range(1, 11): total += card elif card in face: total += 10 else: if total > 11: total += 1 else: total += 11 return total
#check for winner def revealDealerHand(): if len(dealerHand) == 2: return dealerHand(0) elif len(dealerHand) > 2: return dealerHand(0), dealerHand(1)
#game loop
for _ in range(2): dealCard(dealerHand) dealCard(playerHand)
while playerIn or dealerIn: print(f"The dealer had {revealDealerHand} and X") print(f"You have {playerHand} for a total of {total(playerHand)}") if playerIn: stayOrHit = input("1: stay\n2: hit\n") if total(dealerHand) > 16: dealerIn = False else: dealCard(dealerHand) if stayOrHit == '1': playerIn == False else: dealCard(playerHand) if total(playerHand) >= 21: break elif total(dealerHand) >= 21: break
if total(playerHand) == 21: print(f"\nYou have {playerHand} for a total of {playerHand} and the dealer has {dealerHand} for a total of {total(dealerHand)}") print("Blackjack! You win!") elif total(dealerHand) == 21: print(f"\nYou have {playerHand} for a total of {playerHand} and the dealer has {dealerHand} for a total of {total(dealerHand)}") print("Blackjack! Dealer wins!") elif total(playerHand) >21: print(f"\nYou have {playerHand} for a total of {playerHand} and the dealer has {dealerHand} for a total of {total(dealerHand)}") print("You bust. Dealer wins.") elif total(dealerHand) > 21: print(f"\nYou have {playerHand} for a total of {playerHand} and the dealer has {dealerHand} for a total of {total(dealerHand)}") print("Dealer busts. You win.") elif 21 - total(dealerHand) < 21 - total(playerHand): print(f"\nYou have {playerHand} for a total of {playerHand} and the dealer has {dealerHand} for a total of {total(dealerHand)}") print("Dealer wins.") elif 21 - total(dealerHand) > 21 - total(playerHand): print(f"\nYou have {playerHand} for a total of {playerHand} and the dealer has {dealerHand} for a total of {total(dealerHand)}") print("You win.") [/code] Wenn das Programm Zeile 47 erreicht, erhalte ich statt zwei Kartenwerte und eine Gesamtsumme: Der Dealer hatte und X Sie haben [] für eine Gesamtsumme von 0 Ich habe lange versucht herauszufinden, wie ich die Hände des Spielers und des Dealers ausdrucken und die Gesamtsumme des Spielers ausdrucken kann Aber ich kann es für mein Leben nicht herausfinden. Irgendwelche Vorschläge? Ich dachte, ich müsste die Werte der Hände des Spielers und des Dealers in einen Int-Wert umwandeln, aber das funktioniert nicht. Ich übersehe wahrscheinlich etwas wirklich Offensichtliches, aber ich habe ehrlich gesagt keine Ahnung.
Als ich QT verwendete, habe ich einen Unschärfeneffekt auf ein Qwidget angewendet, das ein Fenster ist. Der Code sieht so aus:
#include
#include
#include
int main(int argc, char** argv) {...
Als ich QT verwendete, habe ich einen Unschärfeneffekt auf ein Qwidget angewendet, das ein Fenster ist. Der Code sieht so aus:
#include
#include
#include
int main(int argc, char** argv) {...
Als ich QT verwendete, habe ich einen Unschärfeneffekt auf ein Qwidget angewendet, das ein Fenster ist. Der Code sieht so aus:
#include
#include
#include
int main(int argc, char** argv) {...
Als ich QT verwendete, habe ich einen Unschärfeneffekt auf ein Qwidget angewendet, das ein Fenster ist. Der Code sieht so aus:
#include
#include
#include
int main(int argc, char** argv) {...
Ich habe einen Klickzähler, den ich aus irgendeinem Grund, egal was ich tue, nicht dazu bringen kann, jeden Klick zu zählen. Außerdem möchte ich, dass der Fortschrittsbalken seinen Wert anhand der...