错误提示未定义变量?

Error saying a variable is not defined?

#Intro
import time
import random

def restart():
    bowlSize = random.randint(1,100)
    bowlStrawberries = 0

def lel():
    while bowlSize > bowlStrawberries:
        if (addCheck + bowlStrawberries) > bowlSize:
            print('You´re filling the bowl..')
            time.sleep(2)
            print('...and...')
            time.sleep(2)
            print('Your mom slaps you!')
            restart()


def addStrawberries():
    print('The bowl has ' + str(bowlStrawberries) + ' strawberries in it')
    print('How many strawberries do you want to add?')
    addCheck = input()
    lel()

print('STRAWBERRY (:')
time.sleep(2)
print('Okey so you have a bowl kinda')
time.sleep(2)
print('And you have a bag with 100 strawberries')
time.sleep(2)
print('So ur mom forces you to fill the bowl')
time.sleep(2)
print('But she will slap you if a strawberry drops on the floor')
time.sleep(2)
print('So you have to fill it up in as few tries as possible without overfilling it')
time.sleep(2)

restart()

addStrawberries()

我是编程新手,今天是我的第五天,我不明白为什么会出现错误。您可能有类似的问题,但我是新手,我不知道要搜索什么。当我选择比碗 space.

更高的值时,我基本上希望它重新启动

确切错误:

Traceback (most recent call last):
  File "C:/Users/***/Documents/strenter code herew.py", line 44, in <module>
    addStrawberries()
  File "C:/Users/***/Documents/strw.py", line 27, in addStrawberries
    lel()
  File "C:/Users/***/Documents/strw.py", line 14, in lel
    if (addCheck + bowlStrawberries) > bowlSize:
NameError: name 'addCheck' is not defined

addCheckaddStrawberries 中的局部变量,意味着它在函数外是不可见的。我建议将它作为参数传递给 lel,即调用 lel(addCheck) 并将 lel 定义为 def lel(addCheck)。或者,您可以通过在 addStrawberries 中插入语句 global addCheck 来使 addCheck 成为全局变量,然后再分配给它,但全局变量往往很讨厌。