Python Forum
Help needed for School Assignment
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Help needed for School Assignment
#1
Photo 
so my teacher gave us this assignment, she showed us a video in the class and then we had to replicate it by ourselves. i, unfortunately, do not have that link, but if i were to explain it, we had to print a 'snake' (almost like a parabola, according to my observations and the way in which i had coded it in) from astericks, where the user specifies the number of lines and curvature (in the demo, she used values between (-1,1) for the curvature so i had checked it with those only).

my initial part of the code gives the desired result-

print('Enter the number of lines and curvature')
lines = int(input())
curvature = float(input())

y = lines // 2  

i = -y 
if curvature > 0:
   
  while i <= y:
      x = (i ** 2 / 2) * curvature
      if x >= 0.01:
          spaces = int(x / 0.01) - 2
          print(" " * spaces + "*")
      else:
          print("*")
      i += 0.25
i had assumed the units across x axis as 0.01 and for y axis as 0.25 (no specific reason, i just did so to match it up with the mental image i had from the video we were shown.)

(also can someone explain here why taking i = y and then for each loop assuming i-=0.25 will not give the same output?)
i have attached the image of the output for this.

anyways, this opens like y^2 = 4ax, but i also wanted it to function like y^2 = -4ax, so i added this -

elif curvature < 0:
  while i <= y:
    x = (i ** 2 / 2) * curvature
    if x >= 0.01:
        spaces = (int(((y**2/2)* curvature)/0.01)-2) - (int(x / 0.01) - 2)
        if spaces >= 0:
            print(" " * spaces + "*")
        else:
           print(" " * (-spaces) + "*") # trying to use the modulus value
    else:
        print("*")
    i += 0.25
my reasoning was that the first line should remain now without any space, therefore i subtracted the values in the new spaces. but this pieces gives a straight line of astericks.

can someone correct my logic or give any hints? i am fairly new to python and to this forum, so i would appreciate if a simpler explanation could be given. thank you and apologies if there is any mistake from my side!    
Reply
#2
You are having a problem with math, not Python. Get the math right and the program will be easy to write.

I think your assumptions about axis scaling are wrong. If "lines" is supposed to be the number of "*" printed by the program, "i" should be a number in the range -lines // 2...lines // 2. By integrating "i" by 0.25 you plot 4x the desired number of points. x scaling is also odd. If x == 1, the "*" is printed in column 100, outside then normal width of a terminal (80 columns). I don't think there is any reason to change the scale for x and y. This is your code with all the scaling stuff removed. This only works for if all values x = f(y) >= 0.
lines = int(input("Number of points: "))
curvature = float(input("Curvature: "))
ymax = lines // 2
y = -ymax
while y <= ymax:  # could use for loop.
    x = int((y**2 / 2) * curvature)
    print(" " * x + "*")
    i += 1
The output is a pretty looking curve.
Output:
* * * * * * * * * * * * * * * * * * * * * * *
Your code for curvature < 0 is wrong. Not only wrong in implementation, but logically wrong. curvature <= 0 should not be a special case. Your program has problems if f(y) < 0, not if curvature < 0. If you changed your function to x = y * curvature, some values of x are < 0 and some values >= 0 regardless of the value of curvature. Instead of testing if curvature is negative, your program needs to shift the printing of f(y) such that the minimum value is printed in column 0. To do this you need to find the minimum value for f(y) and use that to shift where "*" is printed. You could compute the minimum value mathematically but for a small number of points it makes more sense to compute all values of f(y) and use the min(values) to get the minimum value. Done properly there will be no special cases (no if statements required).

Computing all values of f(y) also makes it easy to scale your output to fit on the screen. If the leftmost column is 0 and the rightmost column 79, you can compute an offset and scaling factor that convert your f(y) values to the range 0..79. When I add scaling to my program the output looks like this.
Output:
* * * * * * * * * * * * * * * * * * * * * * *
You might also want to control the range for y so you can zoom in on a range of y that is not +/-lines//2. I modified my program to accept a range of y values.
Output:
Number of points: 23 Range (min, max): -1,0 Curvature: -1 * * * * * * * * * * * * * * * * * * * * * * *
keeratkaur likes this post
Reply
#3
(Aug-05-2024, 05:15 PM)deanhystad Wrote: You are having a problem with math, not Python. Get the math right and the program will be easy to write.

I think your assumptions about axis scaling are wrong. If "lines" is supposed to be the number of "*" printed by the program, "i" should be a number in the range -lines // 2...lines // 2. By integrating "i" by 0.25 you plot 4x the desired number of points. x scaling is also odd. If x == 1, the "*" is printed in .........

ahh now i get it! i was too stuck on using a different case of curvature, i missed out on the faultiness for the initial part as well. you are right on the units part, i wasnt getting the curve at that time in the desired shape, so i thought that the stars are there for specific coordinates and i tried to map those on graph ( which was very unproductive Cry ). i worked on the idea that you gave regarding the minima and the values of x, and i think now i have cracked this problem! i made the required changes, and it is giving the wanted output. many thanks! Big Grin
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  school assignment help JKR20 3 730 Feb-25-2025, 08:36 AM
Last Post: buran
  [split] University Assignment Help needed Nomathemba 2 5,847 Apr-07-2019, 11:49 PM
Last Post: micseydel
  Need help with a tough school assignment datson79 5 4,447 Feb-18-2019, 06:01 PM
Last Post: ichabod801
  School Work Assignment: Snake Game DarksideMoses 6 6,750 Apr-29-2018, 10:56 PM
Last Post: DarksideMoses
  University Assignment Help needed Diovanno 3 5,260 Apr-10-2018, 02:46 PM
Last Post: Diovanno
  Array to get profit (school assignment) harryvandervelden 2 3,435 Nov-28-2017, 05:48 PM
Last Post: Larz60+
  School assignment Ronaldo 5 10,435 Jan-08-2017, 11:57 AM
Last Post: Ofnuts

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020