Classes and methods, with lists in Python

2024/11/16 20:33:44

I have two classes, called "Pussa" and "Cat". The Pussa has an int atribute idPussa, and the Cat class has two atributes, a list of "Pussa" and an int catNum. Every class has some methods, with an __init__. So, I've to do this

"(f) Now, our cats get a "Pussa" infection. Put 4 Pussa objects on all even-index cats on the list cats. First generate the 20 needed Pussa objects with consecutive numbers. Then copy the references of the Pussa objects to the corresponding cats."

I have done this piece of code, but I don't really know how to go on... I just started this year with Python and I'm lost.

listPussa = []
for x in range(1,21):x = Pussa(x)listPussa.append(x)for cat in cats: #cats is a list of 20 cat objectsif cat.getcatNum()%2 != 0:for i in range(21):cat.addPussa(i)i = i +1

The class Pussa is defined by:

class Pussa:''' Class Pussa '''# (a) Define a private attribute idPussadef __init__(self, idPussa = None):self.__idPussa = idPussa# (b) Define a __str__ method that def __str__(self):self.__idPussa = idPussaif idPussa is None:return "Pussa sin número."else:return "Pussa número = " + idPussa# (c) Define setIdPussadef setIdPussa(self, idPussa):self.__idPussa = idPussa# (c) Define getIdPussadef getIdPussa(self):return self.__idPussa

And the class Cat:

class Cat:''' Class cat '''# (a) Define the init method with a private attribute called catNum and a list of pussesdef __init__(self, catNum = None, listPussa = []):self.__catNum = catNumself.listPussa = listPussa# (b) Define the mixeta method  def mixeta(self):if len(self.listPussa) is 0:return "Miau!", self.getcatNum(), "La llista de pusses esta buida"else:for pussa in self.listPussa:return pussa.getIdPussa()return  "Miau!", self.getcatNum(), "El nombre total de pusses es ", len(self.listPussa)# (c) Define the setter function for catNumdef setcatNum(self, catNum):self.__catNum = catNum# (c) Define the getter function for catNum     def getcatNum(self):return self.__catNum# (d) Define a setter for adding a Pussa in the list of pussesdef addPussa(self, Pussa):self.listPussa.append(Pussa)# (e) Define the cleanCat method which removes all pusses from the cat, empty the listdef cleanCat(self):if len(self.listPussa) is 0:returnelse:for i in range(len(self.listPussa)):self.listPussa.pop(i)

Every help and comment is very helpful :)

Answer

Probably something like this?

pussaIndex = 0
for cat in cats:if cat.getcatNum() % 2 == 0:# add the next four elements from `listPussa`for i in range(4):cat.addPussa(listPussa[pussaIndex])pussaIndex += 1
https://en.xdnf.cn/q/119150.html

Related Q&A

polars dataframe TypeError: must be real number, not str

so bascially i changed panda.frame to polars.frame for better speed in yolov5 but when i run the code, it works fine till some point (i dont exactly know when error occurs) and it gives me TypeError: m…

Get a string in Shell/Python using sys.argv

Im beginning with bash and Im executing a script :$ ./readtext.sh ./InputFiles/applications.txt Here is my readtext.sh code :#!/bin/bash filename="$1" counter=1 while IFS=: true; doline=read …

Need to combine two functions into one (Python)

Here is my code-def Max(lst):if len(lst) == 1:return lst[0]else:m = Max(lst[1:])if m > lst[0]: return melse:return lst[0] def Min(lst):if len(lst) == 1:return lst[0]else:m = Min(lst[1:])if m < ls…

Error: descriptor blit requires a pygame.Surface object but received a NoneType [duplicate]

This question already has answers here:How can I draw images and sprites in Pygame?(4 answers)Closed 2 years ago.I am creating a game. I wrote this code to create a sprite and its hitbox:hg = pygame.i…

How can I deal with overlapping rectangles? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.Want to improve this question? Add details and clarify the problem by editing this post.Closed 5 years ago.Improve…

panda df not showing all rows after loading from MS SQL

Im using Pandas with latest sqlalchemy (1.4.36) to query a MS SQL DB, using the following Python 3.10.3 [Win] snippet: import pandas as pd # from sqlalchemy…

How to extract multiple grandchildren/children from XML where one child is a specific value?

Im working with an XML file that stores all "versions" of the chatbot we create. Currently we have 18 versions and I only care about the most recent one. Im trying to find a way to extract a…

Is there are a way to replace python import with actual sources?

I am having python files with the import statements which I would like to replace into the actual code placed in the foo.py.For instance, with the in file:from foo import Barbar = Bar() print barI woul…

How to use supported numpy and math functions with CUDA in Python?

According to numba 0.51.2 documentation, CUDA Python supports several math functions. However, it doesnt work in the following kernel function: @cuda.jit def find_angle(angles):i, j = cuda.grid(2)if i …

PYTHON - Remove tuple from list if contained in another list

I have a list of tuples:list_of_tuples = [(4, 35.26), (1, 48.19), (5, 90.0), (3, 90.0)]tuple[0] is an item_IDtuple[1] is an angleI have a list of item_IDs I want to remove/ignore from the list:ignore_I…