Python:图形中的多个数字

时间:2015-12-17 06:49:52

标签: python pseudocode

我试图制作一个图表但是两边有多个数字 19,19,19,19,18,18,18,18,17等 我真正需要的只是y轴上每个数字中的一个 (图中也应该有5颗星{x = 0-x = 4:))

尝试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)+'|'))

2 个答案:

答案 0 :(得分: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),

这将完成所需的工作......如果您还需要其他任何评论......

答案 1 :(得分:0)

我得到了另一个具有所需结果的解决方案:

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)

输出结果为:

   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