India
India

Reputation: 1

How find the max of a list and then store the max in a new list

I am trying to find the max of the "rollList" and everything I have tried isn't working.I'm not very good with coding and the instruction my teacher gave me isn't very clear. I also have to reset "rollList" back to empty for each player and I am very confused.Please someone help.


    import random
    class Player:
        def __init__(self,name ):
            self.name = name
            self.dice = []

        def __str__(self):
            return self.name
        def roll_Dice(self):
            rollDice = random.randint(1, 6)
            return rollDice

    rounds = 1
    rollList = []

    newplayer = []
    newplayer.append(Player("CAT:"))
    newplayer.append(Player("DOG:"))
    newplayer.append(Player("LIZARD:"))
    newplayer.append(Player("FISH:"))

    for rounds in range(1,4):
        print("-----------------")
        print("Round" + str(rounds))
        for p in newplayer:
            print(p)
            for x  in range (4-rounds):
                rollDice = random.randint(1, 6)
                rollList.append(rollDice) 
                print(rollList)
                max.pop(rollList)
                print(rollList)

            rollList.clear()
            len(rollList)

Upvotes: 0

Views: 251

Answers (3)

iGian
iGian

Reputation: 11193

I report some suggestions to resolve the error I suppose you have: AttributeError: 'builtin_function_or_method' object has no attribute 'pop'

Just change max.pop(rollList) to max(rollList).

Then you have a list of only one element because you are calling methods inside the for rounds in range(1,4): loop, without letting the list populate with other elements. You are calling also clear at each loop.

Also, the for x in range (4-rounds): it is not required, it's a nested loop.

You are printing the list of names without assign to each person the value of roll dice, so who's the winner?

Finally, you defined roll_Dice() as instance method of Person, so why not use it? So, why not rollList.append(p.roll_Dice()) instead of:

rollDice = random.randint(1, 6)
rollList.append(rollDice)

Hope this can help.

Upvotes: 0

hk331
hk331

Reputation: 11

You can find the maximum of a list using max() function:

mylist = [1,2,4,5,6,7,-2,3]

max_value = max(mylist)

Now max_value is equal to 7. You can add this to a new list using append() method:

new_list = []
new_list.append(max_value)

then new_list will be [7]

Upvotes: 0

Mad Physicist
Mad Physicist

Reputation: 114310

The line max.pop(rollList) is fairly meaningless. It attempts to call the pop method of the built-in max function, which doesn't exist.

You can get the maximum by just calling max itself:

maxRoll = max(rollList)

If you want to remove that roll, you can (although it doesn't seem necessary, since you'll be clearing the list):

rollList.remove(maxRoll)

If you want to append the maximum to another list:

anotherList.append(maxRoll)

Upvotes: 2

Related Questions