will this work - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Homework (https://python-forum.io/forum-9.html) +--- Thread: will this work (/thread-38063.html) |
will this work - AGM - Aug-29-2022 i made this but i cant test it cause i am on a chromebook and the compiler i used wont work, i want this to give player_ones_hand 7 random cards (bold text is the program i think not sure \'_'/) import random def draw_card(): card_drawn_INT = random.randint(1, 10) if card_drawn_INT <= 4: card_drawn = [infantry] elif card_drawn_INT == [5,6,7,8]: card_drawn = [tank] elif card_drawn_INT == [9,10]: card_drawn = [plane] return card_drawn_INT return card_drawn print(card_drawn_INT) print(card_drawn) def deal_player_one(): player_one_hand = [] for i in(7): draw_card player_one_hand[] = player_one_hand + card_drawn return player_one_hand print(player_one_hand) have indented everything by the way RE: will this work - Yoriz - Aug-29-2022 You could run the code in https://python-forum.io/misc.php?page=interpreter see the comments added to your code import random def draw_card(): card_drawn_INT = random.randint(1, 10) if card_drawn_INT <= 4: card_drawn = [infantry] # infantry not defined elif card_drawn_INT == [5,6,7,8]: # card_drawn_INT is a single item wont == a list, use in card_drawn = [tank] # tank not defined elif card_drawn_INT == [9,10]: # card_drawn_INT is a single item wont == a list, use in card_drawn = [plane] # plane not defined return card_drawn_INT # if you want to return more than one item return them both, return card_drawn_INT, card_drawn return card_drawn # unreachable returns on the previous return # draw_card has not been called and its result is not assigned to a variable print(card_drawn_INT) # card_drawn_INT not defined print(card_drawn) # card_drawn not defined def deal_player_one(): player_one_hand = [] for i in(7): # not how you loop 7 times use range draw_card # not called and result not assigned to a variable card_drawn player_one_hand[] = player_one_hand + card_drawn # invalid assignment of player_one_hand use lists append method, card_drawn not defined return player_one_hand # deal_player_one has not been called and its result is not assigned to a variable print(player_one_hand) # player_one_hand not defined RE: will this work - deanhystad - Aug-29-2022 I don't think you can use random.randint() to draw a card, at least not if you want it to mimic drawing a card from a physical deck. What you've written is more like rolling a die. The difference is that when you draw cards from a deck you remove the cards. Those particular cards cannot be drawn again, and drawing a card changes the probability of what card is drawn next. When rolling a die the next roll is not affected in any way by the previous roll To draw cards from a deck you should create a deck of cards, shuffle the deck, take cards off the top of the deck. import random # Create deck of cards that contains 8 infantry, 8 tanks, 4 planes deck = ["infantry"] * 8 + ["tank"] * 8 + ["plane"] * 4 random.shuffle(deck) print(deck) To draw a card from the deck just pop() off the first card. Now the deck contains 1 less card.
|