So I'm trying to complete this question on my programming course, and it involves drawing stuff with the turtle.
Basically, I'm trying to draw a city skyline, so the program needs to read multiple inputs from the user on one line (the heights of the buildings). I can get it to draw a building, but it only uses the last y-value.
from turtle import *
h = input("Heights: ")
y = h.split()
nxc = -200
#Code for the background
fillcolor("darkslategray")
for i in y:
for i in y:
nyc = i
pencolor("black")
pendown()
begin_fill()
goto(nxc, nyc)
right(90)
forward(20)
right(90)
forward(nyc)
right(90)
forward(20)
right(90)
forward(nyc)
end_fill()
nxc = nxc + 20
Take out your second for
loop:
from turtle import *
h = input("Heights: ")
y = h.split()
nxc = -200
#Code for the background
fillcolor("darkslategray")
for i in y:
nyc = i
pencolor("black")
pendown()
begin_fill()
goto(nxc, nyc)
right(90)
forward(20)
right(90)
forward(nyc)
right(90)
forward(20)
right(90)
forward(nyc)
end_fill()
nxc = nxc + 20
This second loop will always reach the end, updating nyc
each time, before it exits. Thus, for every iteration, nyc
will advance to the final value before Python gets to your drawing code.