Das erste Python -Programm kann Fehler nicht verstehen.Python

Python-Programme
Anonymous
 Das erste Python -Programm kann Fehler nicht verstehen.

Post by Anonymous »

unten ist mein erster ernsthafter Auftrag bei einem Problem der Python 3.13 -Klasse. Ich bin selbst gelehrt. Bitte lassen Sie sich nicht von den Mathematik verschoben. Das Programm ist einfach und die Lösung muss für jemanden mit ein wenig kodierender Erfahrung offensichtlich sein. Ich begann mit allen Variablen, die Klassenvariablen waren und diese bis zu 4 (G, ME, T (Zeit) und DT (Zeitinkrement) haben. Alle anderen sind Instanzvariablen. Es gibt eine Instanz der Klasse namens 'p'. />

Code: Select all

TypeError: Particle.move() takes 4 positional arguments but 5 were given
< /code>
import math
class Particle:
#   CLASS VARIABLES and constants
G = 6.674e-11  # gravity constant kilogram metre second
ME = 5.972e24  # mass earth kg
t = 0  # start time
dt = 1  # time increment seconds

def __init__(self, r, rAng, v, vAng, potE, kinE, totalE):
self.r = r
self.rAng = rAng
self.v = v
self.vAng = vAng
self.potE = potE
self.kinE = kinE
self.totalE = totalE

def move(r, rAng, v, vAng):
a = -Particle.G * Particle.ME / r**2
ax = a * math.cos(rAng)
ay = a * math.sin(rAng)

vx = v * math.cos(vAng) + ax * Particle.dt
vy = v * math.sin(vAng) + ay * Particle.dt
v = (vx**2 + vy**2) ** 0.5
vAng = math.atan2(vy, vx)

rx = r * math.cos(rAng) + vx * Particle.dt + 0.5 * ax * Particle.dt**2
ry = r * math.sin(rAng) + vy * Particle.dt + 0.5 * ay * Particle.dt**2
r = (rx**2 + ry**2) ** 0.5
rAng = math.atan2(ry, rx)
potE = -Particle.G * Particle.ME / r
kinE = 0.5 * 1 * v**2
totalE = potE + kinE
Particle.t += Particle.dt

def result(t, r, rAng, totalE, potE, kinE):
print(
f"t: {Particle.t: >7,d} s\
r: {r/1000:6.3f} km\
rAng: {(rAng*180/3.14159):6.1f}deg\
totalE: {int(totalE*1.0E-6)}MJ\
potE: {int(potE*1.0E-6)}MJ\
kinE:  {int(kinE*1.0E-6)}MJ"
)

p = Particle(12748e03, 0, 5592, math.pi / 2, 0, 0, 0)

while Particle.t < 13000:
p.move(p.r, p.rAng, p.v, p.vAng)
if Particle.t % 100 == 0:
p.result(Particle.t, p.r, p.rAng, p.totalE, p.potE, p.kinE)
else:
continue
Particle.t += Particle.dt
< /code>
There are only 4 arguments. Python is interpreting p.move
als 5. Argument. Ich habe versucht, die Verweise auf P in den Argumenten zu löschen, aber das führt zu mehr Fehlern. Die Mathematik stapelt sich in Ordnung, daher ist es nur ein Fehler, zwischen Partikeln der Klasse und P der Instanz zu verweisen.>

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post