Как мне выстроить свое заявление elif. Между ними есть хороший кусок кода. В то время как, кажется, проблема.
Код очень грязный, так как я очень новичок.
Я поставил много маркеров, чтобы увидеть, где код идет и течет.
#+++++++++++++++++SETUP++++++++++++++++# import random import sys import time # for exiting gracefully PlayersHand = [] DealersHand = [] Ranks = ["2", "3", "4", "5", "6", "7", "8", "9", "10","Ace", "Jack", "Queen", "King"] Suits = ["Hearts", "Diamonds", "Clubs", "Spades"] Deck = [] for num in Ranks: for suit in Suits: card = num + ' of ' + suit Deck += [card] random.shuffle(Deck) print(Deck) for i in Deck: numval=(i[0]) #to get the value of the card(needed first two values for one or ten) #print ("numval is", numval) #+++++++++++++++++INTRODUCTION AND OPTIONS FOR PLAY++++++++++++++++# name = input ("please input your name: ") print("Hi", name, "welcome to Blackjack") print("please choose an option") print("Press 1 to Play Game, 2 to read instructions, 3 to quit") option = int(input("Make your choice: ")); #+++++++++++++++++PLAY THE GAME++++++++++++++++# if option == 1: print ("Lets Play!") def card_value(card): #only reading first slice to determine value of the card if card[:1] in ('J','Q','K','1'): return int(10) elif card[:1] in ('2','3','4','5','6','7','8','9'): #card[:1] example '2' out of the full '2 of Hearts' string return int(card[:1]) elif card[:1] == 'A': print ("\n"+ str(card)) num = input("Do you want this to be 1 or 11?\n>") while num !='1' or num !='11': if num == '1': return int(1) elif num == '11': return int(11) else: num = input("Do you want this to be 1 or 11?\n>") #+++++++++++++++++Deal and display cards++++++++++++++++# PlayersHand = random.choices(Deck, k=2) print(name,"Your cards are", PlayersHand) DealersHand = random.choices(Deck, k=1) print("Dealer, your cards are ** +",DealersHand) total = 0 print("888888888888") #print(PlayersHand.pop(), PlayersHand) card1Val=PlayersHand.pop()[:1] print (card1Val) print("card one value is",card1Val[:1]) card2Val=PlayersHand[:1] listToStr = ' '.join([str(elem) for elem in card2Val]) card2Val = PlayersHand.pop()[:1] print (card2Val) print ("card 2 value is", card2Val) print("gggggggggggggggg") total = 0 if card2Val == "J": total +=10 print (total) elif card2Val == "Q": total +=10 print (total) elif card2Val == "K": total +=10 print (total) else: total +=int(card2Val) print (total) if card1Val == "J": total +=10 print (total) elif card1Val == "Q": total +=10 print (total) elif card1Val == "K": total +=10 print (total) else: total +=int(card2Val) print(total) while int(card1Val+card2Val) <21: decide = input("Do you want to hit or stand?: H or S?") if decide == "H" or "h": PlayersHand.append(random.choices(Deck, k=1)) print ("you got a" , PlayersHand, "your hand is now:", int(card1Val+card2Val) # if dealerHand <17: print ("you've gotta hit again Mr Dealer") elif option == 2: print() print ("These are the instructions, Please read carefully -") print() print (" How to play the game of BlackJack, ") print() print ("Blackjack can be played with a 52-card deck with the jokers removed. In our version only two players will play - the dealer (which is the computer) and the player.") print ("The dealer shuffles the cards and draws two cards, laying one face up and the second face down on the table.") print(" Next, the dealer draws two cards for each player and lays both face up. ") print() print ("After all of the players have received their cards, the game proceeds. The player may request an additional card from the dealer. This is known as a hit. There is no limit to the number of hits that a player may request, so long as the total does not exceed 21. Players may also stand, where they decline additional cards. ") print ("Once the player has either stood or exceeded a total of 21, known as going bust, the dealer reveals the card that it laid face down at the beginning of the game.") print ("The dealer must hit until its total is at least 17, and may go bust. Players who did not bust win the hand if their total is higher than the dealer's or if the dealer goes bust. It is considered a draw if the dealer and player have the same score. ") print() print ("In order to determine their totals, players must add up the values of their individual cards. Numbered cards ranging from two to 10 are valued according to their numbers, while face cards (jacks, queens, and kings) equal 10. The ace card may be counted as 1 or 11, according to the player or dealer's preference.") print() print ("For example, a hand of cards that includes a 10 of clubs, a queen of hearts and an ace of spades would total 21, a winning hand."); print() playChoice = (input("Simple eh? So are you ready to play?: Y or N")); if playChoice == "Y" or "y": option = 1 else: print("Read the instructions again so") elif option == 3: QuitChoice = input ("Are you sure you want to quit?: Y/N") if QuitChoice == "Y" or "y": print("Goodbye!") time.sleep(5) sys.exit() else: print("Great, Lets Play Again") def results(DealersHand, PlayersHand): if total(PlayersHand) == 21: print(PlayersHand) print ("That's a win!") elif total(DealersHand) == 21: print ("Dealers Hand is", DealersHand, "Dealer wins/n") elif total(PlayersHand) > 21: print_results(DealersHand, PlayersHand) print ("BUST") print ('Bye BlackJack world')
Что я уже пробовал:
Выстроились так хорошо, как только могли.
Удалил цикл while, который удаляет ошибку.
Visweswaran N
пожалуйста, отформатируйте свой код и дайте дополнительную информацию о том, где вы столкнулись с этой проблемой, а также вставьте сообщение об ошибке.