1,076,337 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?

Posts by ultimatebuster which have been Voted Up

already knew this

also

from __future__ import braces
ultimatebuster
Posting Whiz in Training
250 posts since Mar 2010
Reputation Points: 24
Solved Threads: 69
Skill Endorsements: 0

simpler: (wrote this this morning but then the internet cut out.)

class Card(object):
    # You can remap these, but then you need to rewrite the __cmp__ function.
    # All the following arranges from small to big
    SUITMAPPER = ("Diamond", "Club", "Heart", "Space")
    VALUEMAPPER = ("2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack",
                   "Queen", "King", "Ace")
    def __init__(self, value, suit):
        # suit and value are both int. They correspondes to the index for the
        # SUITMAPPER and VALUEMAPPER
        self.suit = suit
        self.value = value

    def __cmp__(self, other):
        # check for correct type
        # you can rewrite this function to sort for your own
        if not isinstance(other, Card): 
            raise TypeError("Cannot compare card with non card")
        
        suitcmp = cmp(self.suit, other.suit) # See python doc on cmp.
        
        if suitcmp != 0:
            return suitcmp
        else:
            return cmp(self.value, other.value)

    def __str__(self):
        txt = self.__class__.VALUEMAPPER[self.value]
        txt += " of "
        txt += self.__class__.SUITMAPPER[self.suit]
        return txt


if __name__ == "__main__":
    c_3S = Card(1, 3)
    c_5S = Card(3, 3)
    c_TC = Card(8, 1)
    playerHand = [c_TC, c_3S, c_5S]
    
    print "Unsorted: "
    for card in playerHand:
        print card
        
    playerHand.sort() # .sort() method uses the __cmp__ method
    
    print "Sorted from small to big: "
    for card in playerHand:
        print card

    playerHand.reverse()

    print "Sorted from big to small: "
    for card in playerHand:
        print card
ultimatebuster
Posting Whiz in Training
250 posts since Mar 2010
Reputation Points: 24
Solved Threads: 69
Skill Endorsements: 0

that's simple

txt = "hello world hello world hello world hello world"

def replace_nth_word(n, text):
    text = text.split()
    j = 0
    for i in range(len(text)):
        j += 1
        if j == n:
            j = 0
            text[i] = " "

    return " ".join(text)

print replace_nth_word(3, txt)

""" output:
hello world   world hello   hello world
"""

now you can just open the file and read it and use this function.

ultimatebuster
Posting Whiz in Training
250 posts since Mar 2010
Reputation Points: 24
Solved Threads: 69
Skill Endorsements: 0

I think you should use object oriented approach

I made a fairly simple Quiz

class MyQuiz:
    def __init__(self, questions, answers):
        # both questions and answers should be a list/tuple of string
        self.questions = questions
        self.answers = answers
        
        self.currentround = 0

    def playround(self, roundno):
        # roundno is the round number, otherwise correspond with the index
        # of the question and answer list/tuple.
        
        # don't use round as variable name as it is a function
        return raw_input(self.questions[roundno] + " Your answer: ").lower() == self.answers[roundno].lower()
    
    def mainloop(self):
        while self.currentround < len(self.questions):
            if (self.playround(self.currentround)):
                self.currentround += 1
                print "Your answer is correct!"
            else:
                print "Your answer is wrong!"

        print "You have completed the quiz!"


if __name__ == "__main__":
    q = ("What year is this?", "What is world's number 1 search engine?", "What language is this quiz programmed in?")
    a = ("2010", "Google", "Python")
    newGame = MyQuiz(q, a)
    newGame.mainloop()


""" Sample OUTPUT from the above program:

What year is this? Your answer: 2010
Your answer is correct!
What is world's number 1 search engine? Your answer: yahoo
Your answer is wrong!
What is world's number 1 search engine? Your answer: google
Your answer is correct!
What language is this quiz programmed in? Your answer: c++
Your answer is wrong!
What language is this quiz programmed in? Your answer: python
Your answer is correct!
You have completed the quiz!
"""

Of course you can have multiple answers,that would be as simple as changing the == in the play round to "in" and remove the lower() at the end. You probably need to prepare them by putting the answer to lower case in the beginning, you can also have error check etc.

Adding new feature is just adding new stuff to the playround function at this point, unless you want GUI, which is a bitch and requires more coding, but it just drawing stuff in the playround and __init__ function, for example, changing the raw_input to getting input from a textbox etc.

ultimatebuster
Posting Whiz in Training
250 posts since Mar 2010
Reputation Points: 24
Solved Threads: 69
Skill Endorsements: 0

Panda3D is an free 3D engine that allows Python programming as well as C++. So you win with this engine either way.

ultimatebuster
Posting Whiz in Training
250 posts since Mar 2010
Reputation Points: 24
Solved Threads: 69
Skill Endorsements: 0

Python is extremely uncommon for use with websites. Sure, it's powerful, easy, but when it comes to real website (assuming that's what you want to do), you need php.

ultimatebuster
Posting Whiz in Training
250 posts since Mar 2010
Reputation Points: 24
Solved Threads: 69
Skill Endorsements: 0

use re.split

>>> import re
>>> s = "84, 21"
>>> li = re.split("\D+", s)
>>> li
['84', '21']
>>> for i in range(len(li)):
...     li[i] = int(li[i])
... 
>>> li
[84, 21]
>>> tu = tuple(li)
>>> tu
(84, 21)
>>>
ultimatebuster
Posting Whiz in Training
250 posts since Mar 2010
Reputation Points: 24
Solved Threads: 69
Skill Endorsements: 0

It is in fact true. Nothing is truly private in Python. What happens is that Python alters the name of the variable internally.

>>> class Foo:
	__privatebar = "HelloWorld!"

	
>>> Foo.__privatebar

Traceback (most recent call last):
  File "<pyshell#3>", line 1, in <module>
    Foo.__privatebar
AttributeError: class Foo has no attribute '__privatebar'

>>> Foo._Foo__privatebar
'HelloWorld!'
>>>

But PLEASE! Do not ever do this. Just imagine that the python god will come and hit you if you do this.

This is not weak. Python doesn't encourage bad code, but they don't deny it. If you want to do something stupid like this, don't come here to ask us to debug it for you.

ultimatebuster
Posting Whiz in Training
250 posts since Mar 2010
Reputation Points: 24
Solved Threads: 69
Skill Endorsements: 0

The better way would be using objects

Not sure about the rules on these, but I wrote an article recently on this (and writing another one):
http://thekks.net/916

ultimatebuster
Posting Whiz in Training
250 posts since Mar 2010
Reputation Points: 24
Solved Threads: 69
Skill Endorsements: 0

Is it possible to store a certain function as a variable?

If not, is it okay to make a class, and define a function. Store the class as the variable and invoke that function?

ultimatebuster
Posting Whiz in Training
250 posts since Mar 2010
Reputation Points: 24
Solved Threads: 69
Skill Endorsements: 0
 
© 2013 DaniWeb® LLC
Page rendered in 0.0802 seconds using 2.55MB