有谁知道为什么我不断收到此错误?我真的很新,很感谢别人的帮助。这是我的代码:
import turtle as t
import math as m
import random as r
raindrops = int(input("Enter the number of raindrops: "))
def drawSquare():
t.up()
t.goto(-300,-300)
t.down()
t.fd(600)
t.lt(90)
t.fd(600)
t.lt(90)
t.fd(600)
t.lt(90)
t.fd(600)
t.lt(90)
def location():
x = (r.randint(-300, 300))
y = (r.randint(-300, 300))
t.up()
t.goto(x, y)
return x, y
def drawRaindrops(x, y):
t.fillcolor(r.random(), r.random(), r.random())
circles = (r.randint(3, 8))
radius = (r.randint(1, 20))
newradius = radius
area = 0
t.up()
t.rt(90)
t.fd(newradius)
t.lt(90)
t.down()
t.begin_fill()
t.circle(newradius)
t.end_fill()
t.up()
t.lt(90)
t.fd(newradius)
t.rt(90)
while circles > 0:
if x + newradius < 300 and x - newradius > -300 and y + newradius < 300 and y - newradius > -300:
t.up()
t.rt(90)
t.fd(newradius)
t.lt(90)
t.down()
t.circle(newradius)
t.up()
t.lt(90)
t.fd(newradius)
t.rt(90)
newradius += radius
circles -= 1
area += m.pi * radius * radius
else:
circles -= 1
return area
def promptRaindrops(raindrops):
if raindrops < 1 or raindrops > 100:
print ("Raindrops must be between 1 and 100 inclusive.")
if raindrops >= 1 and raindrops <= 100:
x, y = location()
area = drawRaindrops(x, y)
area += promptRaindrops(raindrops - 1)
return x, y, area
def main():
t.speed(0)
drawSquare()
x, y, area = promptRaindrops(raindrops)
print('The area is:', area, 'square units.')
main()
t.done()
我假设“ + =”出了点问题,但我不知道该怎么办。我相当确定该区域已正确返回。请帮助。:)
我注意到两件事:
promptRaindrops
返回一个元组我确定您并没有打算这样做,但是当您说时area += promptRaindrops(raindrops - 1)
,您要向其中添加一个元组area
,它是一个整数。要解决此问题,您应该说area += promptRaindrops(raindrops - 1)[2]
要返回该区域。但是,您的错误是由
在中promptRaindrops
,您每当返回函数的递归调用1 <= raindrops <= 100
。但是,当它超出该范围时,它不返回任何内容,仅显示一条消息。您的函数将始终在该范围之外,因为如果您继续减小传递给的值promptRaindrops
,则它最终将低于1。当它返回时,您将返回None
(因为未返回任何内容)。在这一点None
上进行的每个递归调用中都会冒出气泡,您将不可避免地将添加None
到area
。添加一个返回元组的return语句,您的错误应消失。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句