1

Starting with the following card game code:

'''CardGame_draw_five1.py
create a deck of cards, shuffle and draw a hand of five cards
suit: club=C, diamond=D, heart=H spade=S
rank: ace=A, 10=T, jack=J, queen=Q, king=K, numbers=2..9
ace of spade would be AS, 8 of heart would be 8H and so on ...
'''

import random

def create_deck():
    """
    create a full deck of cards as a list of codes
    """
    ranks = "A23456789TJQK"
    suits = "CDHS"
    cards = [rank + suit for rank in ranks for suit in suits]
    return cards

def shuffle_deck(cards):
    random.shuffle(cards)
    return cards

def draw_5cards(cards):
    """
    pop five cards of the end of the cards list
    return a list of cards shorten by those cards
    and a list of the 5 cards
    """
    hand5 = cards[len(cards)-5:]
    # shorten the deck by these 5 cards
    del cards[len(cards)-5:]
    cards_left = len(cards)
    if cards_left < 5:
        cards = create_deck()
        cards = shuffle_cards(cards)
        print "A new deck of cards has been shuffled!"
    return cards, hand5


cards = create_deck()
cards = shuffle_deck(cards)
print("Cards in deck = %d" % len(cards))  # test
cards, hand5 = draw_5cards(cards)
print("Cards in deck = %d" % len(cards))  # test
print("Five cards drawn:")
print(hand5)
print("Five cards sorted by rank:")
hand5rank = sorted(hand5, key=lambda x: x[0])
print(hand5rank)
print("Five cards sorted by suit:")
hand5suit = sorted(hand5, key=lambda x: x[1])
print(hand5suit)

'''possible result -->
Cards in deck = 52
Cards in deck = 47
Five cards drawn:
['AH', '4C', 'TS', '6H', '7C']
Five cards sorted by rank:
['4C', '6H', '7C', 'AH', 'TS']
Five cards sorted by suit:
['4C', '7C', 'AH', '6H', 'TS']
'''

Write code to evaluate if you have a valid poker hand.

Votes + Comments
nice approach
2

This is a simple text editor using the Python GUI toolkt PySide:

'''ps_TextEdit_simple_editor1.py
use PySide's QTextEdit and QFileDialog to form a simple
text editor

click right mouse button in edit area for popup menu
for undo, cut, copy. paste, select All, etc.

PySide is the official LGPL-licensed version of PyQT
PySide is available for Windows and UNIX machines
downloaded the free Windows self-extracting installer
for Python27: PySide-1.1.0qt474.win32-py2.7.exe
for Python32: PySide-1.1.0qt474.win32-py3.2.exe
from:
http://developer.qt.nokia.com/wiki/PySide_Binaries_Windows
'''

from PySide.QtCore import *
from PySide.QtGui import *

class MyFrame(QWidget):
    def __init__(self, parent=None):
        QWidget.__init__(self, parent)
        # setGeometry(x_pos, y_pos, width, height)
        self.setGeometry(100, 50, 600, 350)
        self.setWindowTitle('A simple editor using PySide')

        self.edit = QTextEdit(self)
        self.load_button = QPushButton(self)
        self.load_button.setText('Load text file')
        self.save_button = QPushButton(self)
        self.save_button.setText('Save text file')
        self.info_label = QLabel(self)
        s = "click right mouse button in edit area for popup menu"
        self.info_label.setText(s)

        # use grid layout manager
        grid = QGridLayout(self)
        # addWidget(QWidget, row, column, rowSpan, columnSpan)
        grid.addWidget(self.load_button, 0, 0, 1, 1)
        grid.addWidget(self.save_button, 0, 2, 1, 1)
        grid.addWidget(self.edit, 1, 0, 1, 3)
        grid.addWidget(self.info_label, 2, 0, 1, 1)
        self.setLayout(grid)

        # bind the button clicked to action
        # newer connect style used with version 4.5 or higher
        self.load_button.clicked.connect(self.load_file)
        self.save_button.clicked.connect(self.save_file)

    def load_file(self):
        """
        load the selected filename and display in QTextEdit
        get the file name via QFileDialog.getOpenFileName()
        """
        caption = 'Open file'
        # use current/working directory
        directory = './'
        filter_mask = "Python/Text files (*.py *.pyw *.txt)"
        filename = QFileDialog.getOpenFileName(self,
            caption, directory, filter_mask)[0]
        print(filename)  # test
        with open(filename) as fin:
            text = fin.read()
        self.edit.setPlainText(text)
        self.setWindowTitle(filename)

    def save_file(self):
        """
        save text in QTextEdit to a file
        """
        caption = 'Save file'
        # use current/working directory
        directory = './'
        filter_mask = "Python/Text files (*.py *.pyw *.txt)"
        filename = QFileDialog.getSaveFileName(self,
            caption, directory, filter_mask)[0]
        print(filename)  # test
        with open(filename, "w") as fout:
            text = self.edit.toPlainText()
            fout.write(text)
        self.setWindowTitle(filename)


app = QApplication([])
frame = MyFrame()
frame.show()
app.exec_()

See if you can embelish this code by introducing a find/replace text function.

Votes + Comments
+1 for PySide
1

This are the 100 most popular names given to baby girls in the US from 1900 to 1910:
Mary,Helen,Margaret,Anna,Ruth
Elizabeth,Dorothy,Marie,Florence,Mildred
Alice,Ethel,Lillian,Gladys,Edna
Frances,Rose,Annie,Grace,Bertha
Emma,Bessie,Clara,Hazel,Irene
Gertrude,Louise,Catherine,Martha,Mabel
Pearl,Edith,Esther,Minnie,Myrtle
Ida,Josephine,Evelyn,Elsie,Eva
Thelma,Ruby,Agnes,Sarah,Viola
Nellie,Beatrice,Julia,Laura,Lillie
Lucille,Ella,Virginia,Mattie,Pauline
Carrie,Alma,Jessie,Mae,Lena
Willie,Katherine,Blanche,Hattie,Marion
Lucy,Stella,Mamie,Vera,Cora
Fannie,Eleanor,Bernice,Jennie,Ann
Leona,Beulah,Lula,Rosa,Ada
Ellen,Kathryn,Maggie,Doris,Dora
Betty,Marguerite,Violet,Lois,Daisy
Anne,Sadie,Susie,Nora,Georgia
Maude,Marjorie,Opal,Hilda,Velma

... and these are the names for the years 2000 to 2010:
Emily,Madison,Emma,Hannah,Abigail
Olivia,Ashley,Samantha,Alexis,Sarah
Elizabeth,Isabella,Alyssa,Grace,Lauren
Taylor,Jessica,Brianna,Kayla,Sophia
Anna,Natalie,Victoria,Chloe,Sydney
Jasmine,Hailey,Megan,Rachel,Morgan
Julia,Destiny,Ava,Jennifer,Kaitlyn
Mia,Katherine,Alexandra,Haley,Savannah
Nicole,Maria,Allison,Mackenzie,Stephanie
Brooke,Amanda,Ella,Makayla,Faith
Kaylee,Jenna,Andrea,Katelyn,Mary
Jordan,Gabrielle,Rebecca,Paige,Madeline
Kimberly,Trinity,Zoe,Michelle,Sara
Lily,Kylie,Alexa,Caroline,Vanessa
Amber,Angelina,Gabriella,Lillian,Riley
Sierra,Danielle,Leah,Jada,Autumn
Erin,Maya,Ariana,Audrey,Isabel
Sofia,Marissa,Bailey,Jacqueline,Melissa
Claire,Evelyn,Shelby,Jocelyn,Mariah
Avery,Leslie,Melanie,Arianna,Aaliyah

Write a Python program that finds any names that are still popular over a hundred year span.

1

Write a Python program that compares two text strings and gives a percent rating how well they match.

One hint ...

'''str_set_xor1.py
show the words that are in one text or the other
but not in both
'''

text1 = """\
Mary had a little lamb
its fleece was white as snow
and everywhere that Mary went
the lamb was sure to go"""

text2 = """\
Mary has a little lamp
his fleece as white as snow
and where ever that Mary went
the lamp is sure to go"""

# create a set of unique words for text1
set1 = set(text1.split())
# dito for text2
set2 = set(text2.split())

# create a new set with words in either
# set1 or set2 but not both (exclusive or)
set_xor = set1.symmetric_difference(set2)

# show result
print("%d words are different" % len(set_xor))
print('-'*22)
for word in set_xor:
    print(word)

'''result ...
11 words are different
----------------------
his
was
is
everywhere
its
lamb
where
had
lamp
has
ever
'''

Edited by vegaseat

1

Given an amount of less than one Dollar/Euroes, write a Python program to represent this amount such that the least amount of common coins are used.

0

Write a generator that produces the following sequence:
1,2,3,4,5,10,20,30,40,50,100,200,300,400,500

1

A good Project for the mid-way between beginner and intermediate is blackjack. It will get you used to most functions, list, objects, and depending on how serious you want it to be, GUI.

1

Assume you have filed all your code snippets into one directory. Write a Python program that searches this directory for keywords and brings up the names of the files that contain these keywords.

1

Control a turtle graphics canvas (module turtle) from an external Tkinter window with buttons.

0

Assume you have a list of unique integers, write a program that will show the closest pair or pairs.

1

Create a function that shifts elements of a list in circular order.
For instance [1,2,3,4,5] --> [5,1,2,3,4] etc.

1

Using a Python GUI tookit like Tkinter write a numeric seven segment LED display like the ones on your alarm clock.

0

Write a Python program that finds the smallest number that can be divided by each of the numbers from 1 to 10 without leaving a remainder.

2

Compare texts and see which ones are a close match.
Work out a degree/percentage of match.

Edited by vegaseat

0

How about you try to make a calculator to find the chances of landing somewhere on monopoly from you location. What are the chances of getting from Mayfair to Liverpool Street Station in one turn?

4

In Tkinter programme a periodic table where if you click an element it gives you more info.

Votes + Comments
Great idea, I'll definitely try this.
Not so bad idea!
0

You have two strings that look much alike:
waterkraftownermanuals
wadercraftownersmanual

Write Python program that will highlight the differences by capitalizing the characters that don't match:
waTerKraftownermanualS
waDerCraftownerSmanual

0

Generate 2 lists with 25 random numbers, where each list’s numbers are not within 20 digits of each other or others list. Numbers for list X must be less than 1024 and numbers in list Y must be less than 768. For example:

ListX = [80, 140, 1, 1014, etc…
ListY = [ 56, 25, 110, 612, etc…

1

Make a password generator where the user can enter how many characters they won't it to be and if the want symbols or not. Then it will generate passwords with Caps, Lowercase and numbers 0-9.

Hint- Use random number generator in python.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.