class Population:
def __init__(self, m=0, n=0, k=0): # 1
self.m = m
self.n = n
self.k = k
self.encounter = 0
bac = []
bac = bac + [1] * self.m + [2] * self.n + [3] * self.k
self.bac = bac
def plasmids(self): # 2
set = (self.m, self.n, self.k)
return set
def __str__(self): # 3
return "type I: {}, type II: {}, type III: {} (after {}
encounters)".format(self.m, self.n, self.k,
self.encounter)
def __repr__(self): # 4
return "Population({}, {}, {})".format(self.m, self.n, self.k)
def size(self):
return self.m + self.n + self.k
def encounter(self, a, b):
i = self.bac[a]
j = self.bac[b]
if i != j:
if i == 1:
if j == 2:
self.bac[a] = 3
self.bac[b] = 3
elif j == 3:
self.bac[a] = 2
self.bac[b] = 2
elif i == 2:
if j == 1:
self.bac[a] = 3
self.bac[b] = 3
elif j == 3:
self.bac[a] = 1
self.bac[b] = 1
else:
if j == 1:
self.bac[a] = 2
self.bac[b] = 2
elif j == 2:
self.bac[a] = 1
self.bac[b] = 1
self.encounter += 1
>>> population = Population(m=998, n=1, k=1) >>> print(population) type I: 998, type II: 1, type III: 1 (after 0 encounters) >>> print(repr(population)) Population(998, 1, 1) >>> print(population.plasmids()) (998, 1, 1) >>> for i in range(population.size()-1): ... population.encounter(i, i+1) >>> print(population) type I: 997, type II: 0, type III: 3 (after 999 encounters) >>> for i in range(population.size()-1): ... population.encounter(i, i+1) >>> print(population) type I: 997, type II: 3, type III: 0 (after 1998 encounters)
>>population.encounter(i, i + 1)
TypeError: 'int' object is not callable
how can i do when this error came out?
This is because your method name is the same as the variable name counter.
You can overcome it by RENAMING THE METHOD NAME from counter to counter_ as shown in the image below.

You should call encounter() as encounter_()


class Population: def __init__(self, m=0, n=0, k=0): # 1 self.m = m self.n = n self.k...
Here is my code for minesweeper in python and it has something
wrong. Could you please help me to fix it?
import tkinter as tk
import random
class Minesweeper:
def __init__(self):
self.main = tk.Tk()
self.main.title("mine sweeper")
self.define_widgets()
self.mines = random.sample( [(i,j) for i in range(25) for j in
range(50) ],self.CustomizeNumberOfMines())
print(self.mines)
self.main.mainloop()
self.CustomizeNumberOfMines()
def define_widgets(self):
""" Define a canvas object, populate it with squares and
possible texts """
self.canvas = tk.Canvas(self.main, width = 1002, height=502,
bg="#f0f0f0")
self.canvas.grid(row=0, column=0)
self.boxes =...
class Leibniz: def __init__(self): self.total=0 def calculate_pi(self,n): try: self.total, sign = 0, 1 for i in range(n): term = 1 / (2 * i + 1) self.total += term * sign sign *= -1 self.total *= 4 return self.total except Exception as e: ...
COMPLETE THE _CONSTRUCT() AND CONSTRUCTTREE() FUNCTIONS class expressionTree: class treeNode: def __init__(self, value, lchild, rchild): self.value, self.lchild, self.rchild = value, lchild, rchild def __init__(self): self.treeRoot = None #utility functions for constructTree def mask(self, s): nestLevel = 0 masked = list(s) for i in range(len(s)): if s[i]==")": nestLevel -=1 elif s[i]=="(": nestLevel += 1 if nestLevel>0 and not (s[i]=="(" and nestLevel==1): masked[i]=" " return "".join(masked) def isNumber(self, expr): mys=s.strip() if len(mys)==0 or not isinstance(mys, str): print("type mismatch error: isNumber")...
PYTHON
--------------------------------------------------------
class LinkedList:
def __init__(self):
self.__head = None
self.__tail = None
self.__size = 0
# Return the head element in the list
def getFirst(self):
if self.__size ==
0:
return
None
else:
return
self.__head.element
# Return the last element in the list
def getLast(self):
if self.__size ==
0:
return
None
else:
return
self.__tail.element
# Add an element to the beginning of the
list
def addFirst(self, e):
newNode = Node(e) #
Create a new node
newNode.next =
self.__head # link...
Previous code:
class BinarySearchTree:
def __init__(self, data):
self.data = data
self.left = None
self.right = None
def search(self, find_data):
if self.data == find_data:
return self
elif find_data < self.data and self.left != None:
return self.left.search(find_data)
elif find_data > self.data and self.right != None:
return self.right.search(find_data)
else:
return None
def get_left(self):
return self.left
def get_right(self):
return self.right
def set_left(self, tree):
self.left = tree
def set_right(self, tree):
self.right = tree
def set_data(self, data):
self.data = data
def get_data(self):
return self.data
def traverse(root,order):...
Code Example 14-2 class Die: def __init__(self): self.__value = 1 def getValue(self): return self.__value def roll(self): self.__value = random.randrange(1, 7) Refer to Code Example 14-2: Given a Die object named die, which of the following will print the value of the __value attribute to the console? a. print(die.getValue()) b. print(die.roll()) c. print(die.value) d. print(die.__value) To prevent another programmer from directly accessing the attributes of an object, you use a. instantiation b. composition ...
in
python and according to this
#Creating class for stack
class My_Stack:
def __init__(self):
self.items = []
def isEmpty(self):
return self.items == []
def Push(self, d):
self.items.append(d)
def Pop(self):
return self.items.pop()
def Display(self):
for i in reversed(self.items):
print(i,end="")
print()
s = My_Stack()
#taking input from user
str = input('Enter your string for palindrome checking:
')
n= len(str)
#Pushing half of the string into stack
for i in range(int(n/2)):
s.Push(str[i])
print("S",end="")
s.Display()
s.Display()
#for the next half checking the upcoming string...
Code Example 14-3 class Multiplier: def __init__(self): self.num1 = 0 self.num2 = 0 def getProduct(self): return self.num1 * self.num2 def main(): m = Multiplier() m.num1 = 7 m.num2 = 3 print(m.num1, "X", m.num2, "=", m.getProduct()) if __name__ == "__main__": main() Refer to Code Example 14-3: When this code is executed, what does it print to the console? a. 7 X 3 = 0 b. 3 X 7 = 0 c. 7 X 3...
class FileUtility: def __init__ (self, filename): self.__filename = filename def displayFile(self): # use loop structure to read and print each line of the file def main(): #1. create a new file and write the follwing lines to it. # #I never saw a Purple Cow, #I never hope to see one, #But I can tell you, anyhow, #I’d rather see than be one! # #2. create an object of FileUtility class #3. call the object's displayFile method main()
ill thumb up do your best
python3
import random
class CardDeck:
class Card:
def __init__(self, value):
self.value = value
self.next = None
def __repr__(self):
return "{}".format(self.value)
def __init__(self):
self.top = None
def shuffle(self):
card_list = 4 * [x for x in range(2, 12)] + 12 * [10]
random.shuffle(card_list)
self.top = None
for card in card_list:
new_card = self.Card(card)
new_card.next = self.top
self.top = new_card
def __repr__(self):
curr = self.top
out = ""
card_list = []
while curr is not None:...