Hi!

When I print the six card I would like the "player" to be able to pick three out of these cards. Does anyone know how to do this?

Do I create a new list and then append the cards to that list?

Very thankful for any help!

Here's the first bit of code

import random


class Kortlek():
    def __init__(self,colors=[], forms=[], numbers=[], grades=[]):
        self.cardList = []
        for color in colors:
            for form in forms:
                for number in numbers:
                    for grade in grades:
                        self.cardList.append(Kort(color, form, number, grade))

    def spelkorten(self):
        for a in random.sample(self.cardList, 6):
            a.visa()
            



class Kort():
    def __init__(self,color,form,number,grade):
        self.color = color
        self.form = form
        self.number = number
        self.grade = grade
    def visa(self):
        print self.color , "\t", self.form, "  \t" ,self.number,"\t", self.grade

def kortutdelning():
    kort = Kortlek(["blue", "yellow", "red"],["triangel", "circle", "square"],[1, 2, 3],[1, 2, 3])
    kort.spelkorten()

Recommended Answers

All 3 Replies

random.sample() returns a list (of cards in your case).

import random

x = ["a", "b", "c", "d", "e"]
ret = random.sample(x, 2)

print ret, type(ret)

I don't really undertand what you mean...

Here's the entire code, def names are in swedish ;)

import random


class Kortlek():
    def __init__(self,colors=[], forms=[], numbers=[], grades=[]):
        self.cardList = []
        for color in colors:
            for form in forms:
                for number in numbers:
                    for grade in grades:
                        self.cardList.append(Kort(color, form, number, grade))

    def spelkorten(self):
        for a in random.sample(self.cardList, 6):
            a.visa()
            



class Kort():
    def __init__(self,color,form,number,grade):
        self.color = color
        self.form = form
        self.number = number
        self.grade = grade
    def visa(self):
        print self.color , "\t", self.form, "  \t" ,self.number,"\t", self.grade
    



def spelregler():
    print " "
    print "1. Vad går spelet ut på?"
    print "2. Hur får jag poäng?"
    print "3. Tillbaka till huvudmenyn\n"
    spelregelsVal = raw_input("Vilket alternativ väljer du?")
    
    if spelregelsVal == "1":
        print "\nFörst kommer sex olika kort att slumpas ut på spelplanen.\nSpelet går sedan ut på att du ska välja ut tre av\ndessa kort som sedan kommer att jämföras med varann."
        spelregler()         

    elif spelregelsVal == "2":
        print "\nTvå poäng ges om du väljer ut \nTre päng...."
        spelregler()

    elif spelregelsVal == "3":
        meny()


    else:
        print "Välj antingen val 1, 2 eller 3"
        



def kortutdelning():
    kort = Kortlek(["blue", "yellow", "red"],["triangel", "circle", "square"],[1, 2, 3],[1, 2, 3])
    kort.spelkorten()





def fraga(fragan):
    return raw_input (fragan)


def kortval():
    kortval1 = raw_input("Vilka kort väljer du?")






    





def meny():
    print "\n" * 2 + "*" * 80
    print "Hej och välkommen till patiens med en annorlunda kortlek"
    print "*" * 80 + "\n" * 2
    print "1. Börja spela?"
    print "2. Hur spelar jag?"
    print "3. Avsluta?"
    print " "
    return fraga("Vad väljer du? ")

val = "1"

while val != "3":
    val = meny()    
    if val == "1":
        kortutdelning()
        

        val2 = raw_input("Vill du spela igen? Ja/Nej ")

        val2 = "Ja"

        while val2 != "Ja":

            if val2 =="Ja":
                kortutdelning()
                print "Då kör vi! /n"
        
            elif val2 == "Nej":
                quit()
                print "På återseende"

            else:
                print "Välj antingen Ja eller Nej!"


        
 
    elif val == "2":
        spelregler()
        

    elif val == "3":
        print "På återseende"
        quit()

    else:
        print "Välj antingen val 1 eller 2"



    
meny()

First I would like the program to ask the player which three cards to play with. When he has made his pick the three cards will be deleted from the list (del list(somesting)) and three new cards will appear! :) Any help at all is greatly appreciated!

In the future, you would probably receive more answers with a small block of code that illustrates what you want to do, rather than 135 lines of untested code.

val2 = raw_input("Vill du spela igen? Ja/Nej ")
 
        val2 = "Ja"
 
        while val2 != "Ja":

            if val2 =="Ja":

A simple selection example:

import random
import string
choose_from = list(string.lowercase)
for j in range(5):
    choices = random.sample(choose_from, 3)
    print choices, "-->", choose_from, "\n"
    choose_from = [x for x in choose_from if x not in choices]
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.