Python Forum
[split] Teacher (thrown in at the deep end - help)
Thread Rating:
  • 1 Vote(s) - 5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[split] Teacher (thrown in at the deep end - help)
#1
One of the kids in my class (who has obviously taken an interest in programming) has come to me with a problem - I've attempted to work out what the issue is but I've told him I couldn't see it. Can someone have a look please and tell me what the issue is (it is to do with line 82 I think - it's been annotated at that point) - any help would be appreciated Sad

import random

Ace_h = [1, '♥']
Two_h = [2, '♥']
Three_h = [3, '♥']
Four_h = [4, '♥']
Five_h = [5, '♥']
Six_h = [6, '♥']
Seven_h = [7, '♥']
Eight_h = [8, '♥']
Nine_h = [9, '♥']
Ten_h = [10, '♥']
Jack_h = [11, '♥']
Queen_h = [12, '♥']
King_h = [13, '♥']
Ace_c = [1, '♣']
Two_c = [2, '♣']
Three_c = [3, '♣']
Four_c = [4, '♣']
Five_c = [5, '♣']
Six_c = [6, '♣']
Seven_c = [7, '♣']
Eight_c = [8, '♣']
Nine_c = [9, '♣']
Ten_c = [10, '♣']
Jack_c = [11, '♣']
Queen_c = [12, '♣']
King_c = [13, '♣']
Ace_d = [1, '♦']
Two_d = [2, '♦']
Three_d = [3, '♦']
Four_d = [4, '♦']
Five_d = [5, '♦']
Six_d = [6, '♦']
Seven_d = [7, '♦']
Eight_d = [8, '♦']
Nine_d = [9, '♦']
Ten_d = [10, '♦']
Jack_d = [11, '♦']
Queen_d = [12, '♦']
King_d = [13, '♦']
Ace_s = [1, '♠']
Two_s = [2, '♠']
Three_s = [3, '♠']
Four_s = [4, '♠']
Five_s = [5, '♠']
Six_s = [6, '♠']
Seven_s = [7, '♠']
Eight_s = [8, '♠']
Nine_s = [9, '♠']
Ten_s = [10, '♠']
Jack_s = [11, '♠']
Queen_s = [12, '♠']
King_s = [13, '♠']

Deck = [Ace_h, Two_h, Three_h, Four_h, Five_h, Six_h, Seven_h, Eight_h, Nine_h, Ten_h, Jack_h, Queen_h, King_h, Ace_c, Two_c, Three_c, Four_c, Five_c, Six_c, Seven_c, Eight_c, Nine_c, Ten_c, Jack_c, Queen_c, King_c, Ace_d, Two_d, Three_d, Four_d, Five_d, Six_d, Seven_d, Eight_d, Nine_d, Ten_d, Jack_d, Queen_d, King_d, Ace_s, Two_s, Three_s, Four_s, Five_s, Six_s, Seven_s, Eight_s, Nine_s, Ten_s, Jack_s, Queen_s, King_s]
random.shuffle(Deck)

n = 0
while n != 5:
    if Deck[n][0] == 1:
        print('Ace', Deck[n][1])
    elif Deck[n][0] == 11:
        print('Jack', Deck[n][1])
    elif Deck[n][0] == 12:
        print('Queen', Deck[n][1])
    elif Deck[n][0] == 13:
        print('King', Deck[n][1])
    else:
        print(Deck[n][0], Deck[n][1])
    n = n + 1


n = 0
X = 5
while n != 5:
    L = 'False'
    Hand = [Deck[0], Deck[1], Deck[2], Deck[3], Deck[4]]
    print('Do you wish to discard', Hand[n][0], Hand[n][1], '?(Yes/No)')
    ans = input()
    if ans in ['Yes', 'yes', 'YES', 'Y', 'y']:
        Hand[n] = Deck[X] # Value is changing but variable isn't saving
        X = X + 1
        print(Hand)
    n = n + 1

n = 0
while n != 5:
    if Hand[n][0] == 1:
        print('Ace', Hand[n][1])
    elif Hand[n][0] == 11:
        print('Jack', Hand[n][1])
    elif Hand[n][0] == 12:
        print('Queen', Hand[n][1])
    elif Hand[n][0] == 13:
        print('King', Hand[n][1])
    else:
        print(Hand[n][0], Hand[n][1])
    n = n + 1
# Results of program
Output:
5 ♦ 6 ♥ 3 ♣ 6 ♦ 5 ♥ Do you wish to discard 5 ♦ ?(Yes/No) y [[7, '♠'], [6, '♥'], [3, '♣'], [6, '♦'], [5, '♥']] Do you wish to discard 6 ♥ ?(Yes/No) y [[5, '♦'], [11, '♠'], [3, '♣'], [6, '♦'], [5, '♥']] Do you wish to discard 3 ♣ ?(Yes/No) y [[5, '♦'], [6, '♥'], [3, '♦'], [6, '♦'], [5, '♥']] Do you wish to discard 6 ♦ ?(Yes/No) y [[5, '♦'], [6, '♥'], [3, '♣'], [2, '♣'], [5, '♥']] Do you wish to discard 5 ♥ ?(Yes/No) y [[5, '♦'], [6, '♥'], [3, '♣'], [6, '♦'], [12, '♥']] 5 ♦ 6 ♥ 3 ♣ 6 ♦ Queen ♥
Reply
#2
Looks like you just need to define Hand outside the while loop, otherwise it will always intialize with the same values on every iteration.
Hand = [Deck[0], Deck[1], Deck[2], Deck[3], Deck[4]]
while n != 5:
    L = 'False'
    print('Do you wish to discard', Hand[n][0], Hand[n][1], '?(Yes/No)')
    ans = input()
    if ans in ['Yes', 'yes', 'YES', 'Y', 'y']:
        Hand[n] = Deck[X] # Value is changing but variable isn't saving
        X = X + 1
        print(Hand)
    n = n + 1
Reply
#3
The student should learn how to use functions.
This will help him to split tasks into smaller less complex tasks and
it will produce reusable code.

Here an example:

import random
import collections


def get_shuffled_deck():
    """
    Returns an shuffled deck.
    """
    deck = collections.deque()
    # https://docs.python.org/3/library/collections.html#collections.deque
    for color in '♥♣♦♠':
        for number in range(1,14):
            deck.append([number, color])
    random.shuffle(deck)
    return deck


def get_cards(deck, number_of_cards):
    cards =[]
    for n in range(number_of_cards):
        card = deck.popleft()
        cards.append(card)
    return cards


def put_cards(deck, cards):
    """
    Put the cards back to the deck.
    """
    for card in cards:
        deck.append(card)


def put_card(deck, card):
    """
    Put one card back to the deck
    """
    deck.append(card)


def print_cards(cards):
    """
    Print cards
    """
    for card in cards:
        if card[0] == 1:
            print('Ace', card[1])
        elif card[0] == 11:
            print('Jack', card[1])
        elif card[0] == 12:
            print('Queen', card[1])
        elif card[0] == 13:
            print('King', card[1])
        else:
            print(card[0], card[1])


if __name__ == '__main__':
    deck = get_shuffled_deck()
    # get 6 cards, remove them from deck
    player_cards = get_cards(deck, 6)
    # print player_cards in console
    print_cards(player_cards)
It's not the whole functionality.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  which exception should be thrown for a sequence of the wrong length? Skaperen 1 847 Jan-06-2023, 04:13 AM
Last Post: deanhystad
  deep learning python Stevedas 1 1,677 Sep-26-2021, 08:32 AM
Last Post: Yoriz
  Pytest API Post call thrown errors pyseeker 4 3,616 Dec-08-2019, 04:53 PM
Last Post: pyseeker
  Invalid argument error thrown. pyseeker 4 8,614 Sep-10-2019, 07:03 PM
Last Post: pyseeker
  convert List of Dicts into a 2 deep Nested Dict rethink 1 3,196 Aug-23-2019, 05:28 PM
Last Post: ichabod801
  pytest fixture in conftest.py thrown error while in the test file runs OzzieOzzum 1 3,973 Jul-31-2018, 12:12 PM
Last Post: OzzieOzzum
  Teacher (thrown in at the deep end - help) Mr90 5 3,877 May-22-2018, 01:08 PM
Last Post: DeaD_EyE
  Parse XML - how to handle deep levels/hierarchy dwill 8 9,404 Apr-17-2018, 04:17 PM
Last Post: dwill
  Exception not thrown in python3 ONEoo7 7 6,112 Apr-09-2017, 01:58 AM
Last Post: snippsat

Forum Jump:

User Panel Messages

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