我的代码遇到问题,我不知道自己能做些什么。我的代码有一个变量“bheight”,它会自动分割成部分(我已编码并且很好)但是我想要它,所以每当它通过“def Building():”代码时它会将1加到bheight [0]上,所以当它下次循环时它将是bheight [1]。
# Map 2
ggplot() +
geom_sf(data=oregon_sf, aes(fill=hispanic.t), color=NA) +
scale_fill_viridis(option = "inferno", direction=-1) +
facet_grid(cat~var2) +
north(data=oregon_sf, symbol=4, location="bottomleft", scale=.2) +
scalebar(data=oregon_sf, dist = 200, st.size=3, height=0.02, dd2km = TRUE, location="bottomright" , model = 'WGS84')
答案 0 :(得分:0)
更改def建筑代码: -
def Building():
for x in range (0,int(size)):
leo.fillcolor('darkslategrey')
leo.begin_fill()
leo.left(90)
leo.forward(bheight[x])
leo.right(90)
leo.forward(20)
leo.right(90)
leo.forward(bheight[x])
leo.left(90)
leo.end_fill()
你的代码中发生的事情是每次你做-1 + 1的地方都会得到第0个索引的值。而只是添加'x'。
答案 1 :(得分:0)
我同意@ maverick的索引修复,但首先看不到索引的任何需求。而不是:
for x in range (0,int(size)):
...
leo.forward(bheight[x])
真的应该是:
for x in range(size):
...
leo.forward(bheight[x])
为什么不简单地做:
for height in bheight:
...
leo.forward(height)
使用此修复程序和其他各种样式修复的代码:
from turtle import Turtle, Screen
def Water():
leo.penup()
leo.setx(-200)
leo.fillcolor('midnightblue')
leo.begin_fill()
for _ in range(2):
leo.forward(400)
leo.right(90)
leo.forward(150)
leo.right(90)
leo.end_fill()
leo.pendown()
def Building():
leo.fillcolor('darkslategrey')
for height in bheight:
leo.begin_fill()
leo.left(90)
leo.forward(height)
leo.right(90)
leo.forward(20)
leo.right(90)
leo.forward(height)
leo.left(90)
leo.end_fill()
blist = input('Please enter building heights e.g. "50 30 60": ')
bsplit = blist.split()
bheight = list(map(int, bsplit))
screen = Screen()
screen.bgcolor("orange")
leo = Turtle()
Water()
Building()
screen.mainloop()