if a == 'нет' or 'да':) работает следующим образом:a равно 'нет', естественно а неравно 'нет', а содержит строку '1111', НО ПОТОМ идет OR, операнд справа - это строка 'да', естественно Python считает ее за истину true, она ведь непустая!if a == 'нет' or true:, а это значит, что print('ok') выполнится в любом случае, так как, в условии есть часть or true,Pythony плевать на a == 'нет', он видит OR, видит true и выполняет условие, проще говоря, можно выкинуть лишнее, и записать так: if true: - это, все также эквивалентно if a == 'нет' or 'да':if a == 'нет' or 'да': на if a == 'нет' or '':. В этом случае, пустая строка эквивалентна false и при этом,a неравно 'нет', а это значит, что условие не будет выполнено и вы не увидите print('ok').if a == 'да':
print('ok')
elif a == 'нет':
print('net')
else:
print('error')if a == 'да' or a == 'нет':
Задача проверить является ли список палиндромом, я делаю самое банальное, дан список head, я проверяю return head == head[: :-1]def x_here(x,y):
if area[x][y][text] == X:
return True
else:
return Falseif x_here(0,0) and x_here(0,1) and x_here(0,2):
return "X"win_condition_topleft_to_botright = ((0,0),(0,1),(0,2))def check_win_condition(first, second, third):
"""на вход три кортежа, являющиеся координатами игрового поля"""
if x_here(*first) and x_here(*second) and x_here(*third):
return "X"
def winner():
check_win_condition(*win_condition_topleft_to_botright)
check_win_condition(*win_condition_2)
check_win_condition(*win_condition_3)
check_win_condition(*win_condition_4)Ah shit, here we go again
Что я не так делаю с интерфейсами?
namespace Health
{
public interface IHealth
{
void Lose(int amount);
void Restore(int amount);
}
public interface IMutable<out T>
{
T Current { get; }
}
public interface IFinal
{
event Action Over;
}
}using System;
using UnityEngine;
namespace Health
{
public class Health : IHealth, IFinal, IMutable<int>
{
public event Action Over;
private readonly int _max;
private const int Min = 0;
public Health(int max)
{
_max = max;
Current = _max;
}
public int Current { get; private set; }
public void Lose(int amount)
{
SetCurrent(Current - amount);
}
public void Restore(int amount)
{
SetCurrent(Current + amount);
}
private void SetCurrent(int amount)
{
Current = Mathf.Clamp(amount, Min, _max);
if (Current == Min) Over?.Invoke();
}
}
}namespace Health
{
public interface IDamageable
{
void ApplyDamage(int amount);
}
}using UnityEngine;
namespace Health
{
public class Knight : MonoBehaviour, IDamageable
{
[SerializeField] private int _maxHealth = 100;
private Health _health;
private void Awake()
{
_health = new Health(_maxHealth);
}
private void OnEnable()
{
_health.Over += Die;
}
private void OnDisable()
{
_health.Over -= Die;
}
public void ApplyDamage(int amount)
{
_health.Lose(amount);
Debug.Log($"Damaged, hp left - {_health.Current}");
}
private void Die()
{
Debug.Log("Died");
Destroy(gameObject);
}
}
}using UnityEngine;
namespace Health
{
public class Enemy : MonoBehaviour
{
[SerializeField] private int _damage = 50;
private void OnTriggerEnter2D(Collider2D other)
{
if (other.TryGetComponent(out IDamageable damageable))
{
damageable.ApplyDamage(_damage);
}
}
}
}Как перевести код с Pascal на Python?
from math import sqrt
for i in range(289123456, 389123456+1):
sqrtIT = sqrt(i)
numDel = 0
if (round(sqrtIT)) == sqrtIT:
maxDel = 1
for j in range(1, round(sqrtIT)): # Раз ошибка
"""
Собственно, первый цикл 'for i := 289123456 to 389123456 do begin' вы переделали правильно, почему второй так сделали - непонятно
"""
round(sqrtIT)
if (i % j == 0):
if maxDel == 1 and j != 1: # вторая ошибка. В питоне - не равно пишется так: !=
maxDel = i // j # Еще одна ошибка (из коментов)
if (j != round(sqrtIT)): # третья ошибка
numDel += 2
if j * j == i:
numDel += 1
if numDel == 5:
print(i, ' ', maxDel) Если вы дадите какие-то совесть по оформлению или синтаксу кода, я буду вам нереально благодарен.
я тот же sqlite выучил за 1 день
cursor.execute(f"INSERT INTO {table}({columns}) VALUES ({values})")