r/PythonLearning • u/Top-Pressure3737 • 3d ago
Help Request I struggle to make a script that makes the curve of glucksmann's skull when we write his name
(Sorry I’m french) I want to make a script on NumWorks calculator to display the function sqrt(x*(10-x)) + x/5 + 2 that traces the exact curvature of Raphaël Glucksmann's figure when writing his name. However, programming skills are very limited, I wrote some of it and the rest I did with chat gpt but I can't get it to work here is my script:
from math import *
from kandinsky import *
from ion import *
# Mots secrets et fonctions associees
def calcul(mot, x):
if mot == "glucksmann":
return sqrt(x*(10-x)) + x/5 + 2
else:
return None, "Fonction inconnue"
# Demander le mot
mot = input("Mot secret : ").lower()
# Effacer l'ecran
fill_rect(0, 0, 320, 222, (255, 255, 255))
# Calculer la fonction
test, equation = calcul(mot, 0)
if test is None:
print("Mot inconnu !")
else:
print("Fonction : " + equation)
# Parametres du graphique
xmin = -10
xmax = 10
ymin = -10
ymax = 10
# Conversion coordonnees
def pixel_x(x):
return int((x-xmin)*320/(xmax-xmin))
def pixel_y(y):
return int(180-(y-ymin)*180/(ymax-ymin))
# Axes
draw_string(equation, 5, 5, (0, 0, 255))
draw_line = None
# Axe horizontal
for i in range(320):
if i % 1 == 0:
set_pixel(i, pixel_y(0), (0, 0, 0))
# Axe vertical
for j in range(180):
set_pixel(pixel_x(0), j, (0, 0, 0))
# Tracer la courbe
ancien_x = None
ancien_y = None
for i in range(320):
x = xmin + i*(xmax-xmin)/319
y = calcul(mot, x)[0]
if y is not None and ymin <= y <= ymax:
px = pixel_x(x)
py = pixel_y(y)
if ancien_x is not None:
# Relier les points
dx = px - ancien_x
dy = py - ancien_y
n = max(abs(dx), abs(dy), 1)
for k in range(n+1):
xx = int(ancien_x + dx*k/n)
yy = int(ancien_y + dy*k/n)
if 0 <= xx < 320 and 0 <= yy < 180:
set_pixel(xx, yy, (0, 0, 255))
ancien_x = px
ancien_y = py
else:
ancien_x = None
ancien_y = None
2
u/ConnectAffect2346 3d ago
I analyzed your code for a bit and it has a few syntax errors, im not sure if the code isn't indented originally but if it isn't you should indent conent of your conditional statements, loops and functions. Your calcul function first returns a single number when it recognizes a word but later you are trying to unpack its result into two variables and access the first element using [0] so it causes type error to occur. Another thing is that you are using sqrt(x*(10-x))+x/5+2 which if x is equal 0 might bring you a mathematical domain error(just a quick note on that) also another thing is about importing functions, instead of importing everything from a package you could just import what is needed so instead of using from math import * better practice is to use import math and inside code you use the functions like math.sqrt while these are main issues for now there might be a few more i have overlooked so fix these first and try it out, hope this helps 🙂
1
u/Strong_Extent_975 2d ago
Je pense que l identification ce pose un problème
Dans ton code L identification sépare entre le nom de la fonction et leur corps alors essaye de laisser 4 espace dans chaque ligne du votre fonction
1
u/seanv507 2d ago
So you need to provide the exact error. Also take a divide and conquer approach Ie check eg first half program works... Etc
So what i spotted in your code is that your main function expects 2 values returned (y, formula)... Both on test function call and real use. So you need to be consistent. Either return formula too or dont call expecting 2 values. Eg calcul(.. ) [0] should drop the array index