david

All generations.

fix invalid codeThu, 26 Sep 2024

What is my mistake? My code: class RomanNumerals: @staticmethod def to_roman(val : int) -> str: otvet = '' while val>=1000: otvet += "M" val -= 1000 while val>= 900: otvet += "CM" val -= 900 while val>= 500: otvet += "D" val -= 500 while val >= 400: otvet += "CD" val -= 400 while val>= 100: otvet += "C" val -= 100 while val>= 90: otvet += "XC" val -= 90 while val>= 50: otvet += "L" val -= 50 while val>= 40: otvet += "XL" val -= 40 while val>= 10: otvet += "X" val -= 10 while val>= 9: otvet += "IX" val -= 9 while val>= 5: otvet += "V" val -= 5 while val>= 4: otvet += "IV" val -= 4 while val>= 1: otvet += "I" val -= 1 return otvet @staticmethod def from_roman(roman_num: str) -> int: otvet = 0 while 'CM' in roman_num: otvet += 900 roman_num = roman_num.replace('CM', '', 1) while 'CD' in roman_num: otvet += 400 roman_num = roman_num.replace('CD', '', 1) while 'XC' in roman_num: otvet += 90 roman_num = roman_num.replace('XC', '', 1) while 'XL' in roman_num: otvet += 40 roman_num = roman_num.replace('XL', '', 1) while 'IV' in roman_num: otvet += 4 roman_num = roman_num.replace('IV', '', 1) while 'M' in roman_num: otvet += 1000 roman_num = roman_num.replace('M', '', 1) while 'D' in roman_num: otvet += 500 roman_num = roman_num.replace('D', '', 1) while 'C' in roman_num: otvet += 100 roman_num = roman_num.replace('C', '', 1) while 'L' in roman_num: otvet += 50 roman_num = roman_num.replace('L', '', 1) while 'X' in roman_num: otvet += 10 roman_num = roman_num.replace('V', '', 1) while 'V' in roman_num: otvet += 5 roman_num = roman_num.replace('V', '', 1) while 'I' in roman_num: otvet += 1 roman_num = roman_num.replace('I', '', 1) return otvet print(RomanNumerals.to_roman(86 )) print(RomanNumerals.from_roman('XXI'))

You are not replacing the string correctly. You are replacing the first occurrence of the string, but you want to replace all occurrences. You can use the g flag to replace all occurrences: $('#myDiv').html($('#myDiv').html().replace(/(\<\/?)(div|p)/g, "$1clearfix$2"

fix invalid codeTue, 10 Sep 2024

import tkinter as tk from random import shuffle class MyButton(tk.Button): def __init__(self, master,x,y,num,*args,**kwargs): super().__init__(master,*args,**kwargs, width=3, font='Calibri 15 bold') self.x = x self.y = y self.num = num self.is_mine = False def __repr__(self): return f"MyButton: {self.x} {self.y}; {self.num} {self.is_mine}" class MineSweeper: app = tk.Tk() ROW = 16 COLUMNS = 16 MINES = round(ROW*COLUMNS*0.2) def __init__(self): self.buttons = [] count = 1 for i in range(MineSweeper.ROW): temp = [] for j in range(MineSweeper.COLUMNS): btn = MyButton(MineSweeper.app,x=i,y=j,num=count) btn.config(command=lambda button = btn:self.click(button)) temp.append(btn) count +=1 self.buttons.append(temp) def click(self,clicked_button:MyButton): if clicked_button.is_mine: img = tk.PhotoImage(file="unnamed.png") clicked_button.config(image=img) else: clicked_button.config(text=clicked_button.num) def create_widgets(self): for i in range(MineSweeper.ROW): for j in range(MineSweeper.COLUMNS): btn = self.buttons[i][j] btn.grid(row=i, column=j) def print_buttons(self): for btn in self.buttons: print(btn) def start(self): self.create_widgets() self.insert_mines() self.print_buttons() MineSweeper.app.mainloop() @staticmethod def get_mines_places(): indexes = list(range(1,MineSweeper.ROW*MineSweeper.COLUMNS+1)) shuffle(indexes) return indexes[:MineSweeper.MINES] def insert_mines(self): index_mines = self.get_mines_places() print(index_mines) for row_btn in self.buttons: for btn in row_btn: if btn.num in index_mines: btn.is_mine = True def get_button_size(self): first_button = self.buttons[0][0] first_button.update_idletasks() # Обновляем геометрию button_width = first_button.winfo_width() button_height = first_button.winfo_height() print(f"Размер кнопки: {button_width}x{button_height}") game = MineSweeper() print(game.MINES) game.create_widgets() game.get_button_size() # В game.start()

Проблема в том, что в методе click вы используете this, который в данном случае является объектом, а не контекстом. Поэтому вам нужно передавать в метод click контекст, например, через конст

Questions about programming?Chat with your personal AI assistant