[Python] How can i resolve a jose portilla milestone project 2 infinite loop problem? [closed]

Discussão em 'Python' iniciado por Stack, Maio 14, 2025.

  1. Stack

    Stack Membro Participativo

    I´m following a course of jose portilla (udemy) for python. so i´m making a milestone project 2. idk where i fail in the code to make a infinite loop.

    #agrego valores de las cartas
    import random
    suits = ('hearts','diamonds','spades','clubs')
    ranks =('two','three','four','five','six','seven','eight','nine','ten','jack','queen','king','ace')
    values = {
    'two':2,
    'three':3,
    'four':4,
    'five':5,
    'six':6,
    'seven':7,
    'eight':8,
    'nine':9,
    'ten':10,
    'jack':11,
    'queen':12,
    'king':13,
    'ace':14
    }
    #Establezco como es la carta


    class Card:
    def __init__(self,suit,rank):
    self.suit=suit
    self.rank=rank
    self.value = values[rank]

    def __str__(self):
    return self.rank +" of " + self.suit
    #establezco un mazo
    class Deck:
    def __init__(self):
    self.all_cards = []

    for suit in suits: #itinerancia a suit
    for rank in ranks: #itinerancia a rank
    #crea el objeto de la carta
    created_card = Card(suit,rank) #las uno
    self.all_cards.append(created_card) #crea todas las cartas
    def shuffle(self):#mezclo las cartas
    random.shuffle(self.all_cards)
    def deal_one(self):#elimino cartas
    return self.all_cards.pop()

    class Player:
    def __init__(self,name):

    self.name=name
    self.all_cards= []

    def remove_one(self):

    return self.all_cards.pop(0)

    def add_cards(self,new_cards):
    if type(new_cards) == type ([]):
    self.all_cards.extend(new_cards)
    else:
    self.all_cards.append(new_cards)

    def __str__(self):
    return f'player {self.name} has {len(self.all_cards)} cards '



    new_player = Player("jose")
    new_deck = Deck()

    bottom_card= new_deck.all_cards[-1]



    new_deck.shuffle()

    my_card = new_deck.deal_one()

    new_player.add_cards(my_card)
    print (my_card)
    print(new_player)



    #game setup

    player_one = Player("one")
    player_two = Player("two")

    new_deck = Deck()

    new_deck.shuffle()

    for x in range(26):
    player_one.add_cards(new_deck.deal_one())
    player_two.add_cards(new_deck.deal_one())

    game_on = True


    round_num= 0
    while game_on:

    round_num +=1
    print(f"Round {round_num}")

    if len(player_one.all_cards) == 0:
    print('player one lose, player two wins')
    game_on = False
    break

    if len(player_two.all_cards) == 0:
    print('player two lose, player one wins')
    game_on = False
    break

    player_one_cards = []
    player_one_cards.append(player_one.remove_one)

    player_two_cards = []
    player_two_cards.append(player_two.remove_one)

    at_war= True

    while at_war:

    if player_one_cards[-1].value > player_two_cards[-1].value:

    player_one.add_cards(player_one_cards)
    player_one.add_cards(player_two_cards)

    at_war= False

    elif player_one_cards[-1].value < player_two_cards[-1].value:

    player_two.add_cards(player_one_cards)
    player_two.add_cards(player_two_cards)

    at_war= False

    else:
    print('war!')

    if len(player_one.all_cards) < 5:
    print("player one uneable to declare the war")
    print("player two win")

    game_on=False
    break


    elif len(player_two.all_cards) < 5:
    print("player two uneable to declare the war")
    print("player one win")

    game_on=False
    break


    for num in range(5):
    player_one_cards.append(player_one.remove_one())
    player_two_cards.append(player_two.remove_one())


    i need help to fix this problem and look why i have a infinite loop

    Continue reading...

Compartilhe esta Página