Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old May 2nd, 2005, 8:45 PM   #11
Ooble
I eat cake for breakfast.
 
Ooble's Avatar
 
Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9 Ooble is on a distinguished road
Sane, I don't know if you can convert lists like that, but I'd assume it's not so, judging by your code.
__________________
Me :: You :: Them
Ooble is offline   Reply With Quote
Old May 2nd, 2005, 8:50 PM   #12
hydroxide
Programmer
 
Join Date: Apr 2005
Posts: 73
Rep Power: 4 hydroxide is on a distinguished road
Quote:
Originally Posted by Sane
It's usually a good idea to read all the posts before making yours.
*boggle* Are you responding to me? If so perhaps you should read what I posted more carefully.

--OH.
hydroxide is offline   Reply With Quote
Old May 2nd, 2005, 9:02 PM   #13
Sane
Programming Guru
 
Sane's Avatar
 
Join Date: Apr 2005
Location: Waterloo, Ontario
Posts: 1,868
Rep Power: 5 Sane will become famous soon enough
Send a message via MSN to Sane
Quote:
Originally Posted by hydroxide
*boggle* Are you responding to me? If so perhaps you should read what I posted more carefully.

--OH.
Yes, I was. I guess I don't get your post.

I need to figure out why appending a float value suddenly changes it to 10 decimal places. Not how to sort a randomly generated list.
Sane is offline   Reply With Quote
Old May 2nd, 2005, 10:33 PM   #14
hydroxide
Programmer
 
Join Date: Apr 2005
Posts: 73
Rep Power: 4 hydroxide is on a distinguished road
Quote:
Originally Posted by Sane
Yes, I was. I guess I don't get your post.

I need to figure out why appending a float value suddenly changes it to 10 decimal places. Not how to sort a randomly generated list.
The response was to your initial post - reversed() and sorted() return modified copies of a list while the reverse() and sort() methods provide inplace modification and return None. The responses that you'd previously received to your question were correct for 2.3- but inferior for 2.4+:
x = [4,2,1,3]
print sorted(x), x
print reversed(x), x
print x.reverse(), x
print x.sort(), x

As for your other problem, it's to do with the difference between repr() and str():
from __future__ import division
x = 182/10
print x
print str(x)
print repr(x)
Floating point numbers rarely provide an exact representation - If you need exactitude, you could use the decimal module from 2.4

see Tim Peter's guide to floats

--OH
hydroxide is offline   Reply With Quote
Old May 3rd, 2005, 11:16 AM   #15
Sane
Programming Guru
 
Sane's Avatar
 
Join Date: Apr 2005
Location: Waterloo, Ontario
Posts: 1,868
Rep Power: 5 Sane will become famous soon enough
Send a message via MSN to Sane
Ah, I have 2.3. I didn't realise that the first posts wouldn't work on 2.4, understood. But I still don't understand why when I print b, first alone, comes out as a 1 decimal digit. But as soon as I append it to a list it changes.

?
Sane is offline   Reply With Quote
Old May 3rd, 2005, 12:33 PM   #16
Dietrich
Professional Programmer
 
Dietrich's Avatar
 
Join Date: Feb 2005
Posts: 434
Rep Power: 4 Dietrich is on a distinguished road
Unhappy

Quote:
Originally Posted by hydroxide
If you're using python 2.4:
mylist = [random.randrange(1,6) for i in range(5)]
mysortedlist = reversed(sorted(mylist))
print mysortedlist

--OH.
All I get as an output is:

<listreverseiterator object at 0x01191AD0>

What gives?
__________________
I looked it up on the Intergnats!
Dietrich is offline   Reply With Quote
Old May 3rd, 2005, 1:24 PM   #17
al1986
Newbie
 
Join Date: Feb 2005
Posts: 24
Rep Power: 0 al1986 is on a distinguished road
The reversed() function returns an iterator object. Try invoking a next() method on this object. Alternatively, you can loop through the items by doing
for i in mysortedlist: print i
al1986 is offline   Reply With Quote
Old May 3rd, 2005, 3:47 PM   #18
Sane
Programming Guru
 
Sane's Avatar
 
Join Date: Apr 2005
Location: Waterloo, Ontario
Posts: 1,868
Rep Power: 5 Sane will become famous soon enough
Send a message via MSN to Sane
Okay. I finished it. The ratio variable needs a bit of tweaking depending on the ranges of speed you put in, but overall it's pretty good. Any more suggestions or noticable changes it needs?

def return_turns(party, enemy, ratio):

    """Declare an array of values carrying the character
    number and speed, for all characters"""
    SpeedSet = []    
    Set = []    
    for a in range(len(party)):
        Set.append(10*party[a][1]+party[a][0])
        b= Set[a]
        SpeedSet.append(b)
    Set = []           
    for a in range(len(enemy)):
        Set.append(10*enemy[a][1]+enemy[a][0])
        b= Set[a]
        SpeedSet.append(b)

    """Sort from greatest to least to more easily manage"""
    SpeedSet.sort()
    SpeedSet.reverse()

    """Assign a ratio value based on the difference from
    the maximum speed, including the ratio value inputted
    in the main function argument"""
    NewSet = []
    for a in range(len(SpeedSet)):
        NewSet.append( int((( float( int( str(max(SpeedSet)) [:len(str(max(SpeedSet)))-1] ) - int( str(SpeedSet[a]) [:len(str(SpeedSet[a]))-1] )) / (int(max(SpeedSet))/ratio)+1) *100)+1 ))

    """Eliminate odd ratio values"""
    for a in range(len(NewSet)):
        if NewSet[a]%2 == 0:
            NewSet[a]+=1
            
    """Assign turns based on the divisibility of one's ratio"""
    Turns = []
    for b in range(10000):
        for a in range(len(NewSet)):
            if (b+1)%NewSet[a] == 0: Turns.append(str(SpeedSet[a])[len(str(SpeedSet[a]))-1])

    """End function"""
    #return Turns #REMOVE POUND SYMBOL IN THIS LINE WHEN FINISHED


    #################################################################
    player1 = 0
    player2 = 0
    enemy1 = 0
    enemy2 = 0
    
    for a in range(len(Turns)):
        if Turns[a] == "1": player1 += 1
        if Turns[a] == "2": player2 += 1
        if Turns[a] == "3": enemy1 += 1
        if Turns[a] == "4": enemy2 += 1
    print "Player 1: Gets %d Turns, With %s Speed."%(player1, str(party[0][1]))
    print "Player 2: Gets %d Turns, With %s Speed."%(player2, str(party[1][1]))
    print "Enemy 1: Gets %d Turns, With %s Speed."%(enemy1, str(enemy[0][1]))
    print "Enemy 2: Gets %d Turns, With %s Speed."%(enemy2, str(enemy[1][1]))
    
    print Turns
    #################################################################

   
##########################################
player1 = [1, 1]
player2 = [2, 5]
enemy1 = [3, 50]
enemy2 = [4, 100]
party = [player1, player2]
enemy = [enemy1, enemy2]

ratio = 15
turns = return_turns(party, enemy, ratio)
##########################################

By the way, it's intended to return in advance the order in which the characters' take their turns in my RPG.

Last edited by Sane; May 3rd, 2005 at 5:47 PM.
Sane is offline   Reply With Quote
Old May 3rd, 2005, 7:19 PM   #19
hydroxide
Programmer
 
Join Date: Apr 2005
Posts: 73
Rep Power: 4 hydroxide is on a distinguished road
Quote:
Originally Posted by al1986
The reversed() function returns an iterator object. Try invoking a next() method on this object. Alternatively, you can loop through the items by doing
for i in mysortedlist: print i
Oops... <blush> That's what I get for doing things without having 2.4 on hand ;-P. What I should have posted was:
mysortedlist = list(reversed(sorted(mylist)))

Sorry, --OH.
hydroxide is offline   Reply With Quote
Old May 4th, 2005, 1:06 AM   #20
hydroxide
Programmer
 
Join Date: Apr 2005
Posts: 73
Rep Power: 4 hydroxide is on a distinguished road
Quote:
Originally Posted by Sane
Okay. I finished it. The ratio variable needs a bit of tweaking depending on the ranges of speed you put in, but overall it's pretty good. Any more suggestions or noticable changes it needs?
This is a prime situation where objects would be useful - I'd do something along the lines of:
class Actor(object):
    def __init__(self, name, speed):
        self.speed = speed
        # create the generator and bind the next method to self.update
        # so that whenever self.update() is called, it calls the next method
        # on the generator
        self.update = self._update().next   
        self.name = name

    def act(self):
        print "   >", self.name
        
    def _update(self):
        while True:
            for i in range(self.speed):
                yield None
            yield self.act   # return a reference which is called later

john = Actor("John", 5)
mary = Actor("Mary", 8)
goblin1 = Actor("Goblin1", 7)
goblin2 = Actor("Goblin2", 6)

actors = [john, mary, goblin1, goblin2]
for i in range(50):
    print "Turn",i+1
    for actor in actors:
        func = actor.update()
        if func:            # If actor.update() doesn't return None...
            func()          # call the function (ie self.act) it returns
Note how each object maintains its own state - and determines when and how it acts...

--OH.
hydroxide is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 12:44 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC