![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#11 |
|
I eat cake for breakfast.
![]() ![]() ![]() ![]() Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9
![]() |
Sane, I don't know if you can convert lists like that, but I'd assume it's not so, judging by your code.
|
|
|
|
|
|
#12 | |
|
Programmer
Join Date: Apr 2005
Posts: 73
Rep Power: 4
![]() |
Quote:
--OH. |
|
|
|
|
|
|
#13 | |
|
Programming Guru
![]() |
Quote:
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. ![]() |
|
|
|
|
|
|
#14 | |
|
Programmer
Join Date: Apr 2005
Posts: 73
Rep Power: 4
![]() |
Quote:
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) see Tim Peter's guide to floats --OH |
|
|
|
|
|
|
#15 |
|
Programming Guru
![]() |
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.
? |
|
|
|
|
|
#16 | |
|
Professional Programmer
Join Date: Feb 2005
Posts: 434
Rep Power: 4
![]() |
Quote:
<listreverseiterator object at 0x01191AD0> What gives?
__________________
I looked it up on the Intergnats! |
|
|
|
|
|
|
#17 |
|
Newbie
Join Date: Feb 2005
Posts: 24
Rep Power: 0
![]() |
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 |
|
|
|
|
|
#18 |
|
Programming Guru
![]() |
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. |
|
|
|
|
|
#19 | |
|
Programmer
Join Date: Apr 2005
Posts: 73
Rep Power: 4
![]() |
Quote:
mysortedlist = list(reversed(sorted(mylist))) Sorry, --OH. |
|
|
|
|
|
|
#20 | |
|
Programmer
Join Date: Apr 2005
Posts: 73
Rep Power: 4
![]() |
Quote:
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--OH. |
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|