-1

I have tried to make a graph but the sides have multiple numbers like 19,19,19,19,18,18,18,18,17 etc what I really need is only one of each number on the y axis (Also there supposed to be 5 stars in the graph{x=0-x=4:))

attempt 1

##David Jeon
##Dec 16, 2015
##A program to plot the graph of y = x^2 + 3 using formatted output
##ICS201
##Graph from x = 0 to x = 4
##program to create a y=x^2+3 graph
print('{0:>{width}}'.format('y', width=2))
x = 4
y = x**2+3
oldY = y
for x in range(4,-1,-1):
    print('{0:>3}{1:>{width}}'.format(str(y)+'|','*', width=x*2))
    y = x**2+3
    difference = oldY - y
    for lines in range(0,difference+1):
        print('{0:>3}'.format(str(y)+'|'))
SweetTech
  • 17
  • 5

2 Answers2

0
    print('{0:>{width}}'.format('y', width=2))
x = 4
y = x**2+3
oldY = y
for x in range(4,-1,-1):
    print('{0:>3}{1:>{width}}'.format(str(y)+'|','*', width=x*2))
    y = x**2+3
    difference = oldY - y


print('{0:>{width}}'.format('x|', width=2)),

for x in range(4,-1,-1):
    print (4-x),

This will do required work ... If you want anything else comment ...

Ankur Jyoti Phukan
  • 785
  • 3
  • 12
  • 20
0

I got another solution with the desired result:

xs = []
ys = []
x_axe = '  '
for x in range(5):
    y = pow(x, 2) + 3
    ys.append(y)
    x_axe += ' ' + str(x)
    xs.append(x)

print("""   Y
   ^
   |""")

index = None
for y in range(max(ys), 0, -1):
    y_scale = str(y).zfill(2) + ' |'
    if y in ys:
        index = ys.index(y)
        if xs[index] == 0:
            print(str(y).zfill(2) + ' *')
        else:
            print(y_scale + ('  ' * xs[index])[:-1] + '*')
    else:
        print(y_scale)

print('00 +--' + ('--' * max(xs)) + '> X')
print(x_axe)

The output is:

   Y
   ^
   |
19 |       *
18 |
17 |
16 |
15 |
14 |
13 |
12 |     *
11 |
10 |
09 |
08 |
07 |   *
06 |
05 |
04 | *
03 *
02 |
01 |
00 +----------> X
   0 1 2 3 4
Alderven
  • 7,569
  • 5
  • 26
  • 38
  • Thanks! I will have to check though if this coding is usable though. I have only learnt formatted ouput. Loop for count in range print simple coding^^ – SweetTech Dec 17 '15 at 08:07
  • I will edit my coding tomorrow and post the edited version attempt 2. Hope you will be there to help me^^ – SweetTech Dec 17 '15 at 08:12
  • http://stackoverflow.com/questions/34337245/python-number-fail-in-graph-attempt-2 – SweetTech Dec 17 '15 at 14:47