未定义全局名称列表.只是模块变量 [英] global namelist is not defined. Is only module variable
问题描述
好的,所以使用此代码我进行了一些测试以查看主要问题是什么.我发现电话号码输入验证有问题,所以我#'ed它,所以它不活动.(如果你能解决这个问题,会给你一个贴纸.总之:
Ok so using this code I ran some testing to see what the main problem was. I found there is a problem with the phone number input validation so i #'ed it ,so its inactive.(if your are able to fix that problem wil lgive you a sticker. Anyway:
def main():
name = []
phone = []
groupno = []
score = []
review = []
q = input('Do you wish to Continue Y/N')
while q != "Y":
def data(name, phone, groupno, score, namelist, groupsizes,phonumber, mealscore):
name = input("Please enter the customer's name.")
namelist.append(name)
#phone = int(input("Please enter the customer's Phone Number."))
#if len.str(phone) == 11:
# else: phone = int(input("Please enter the customer's Phone Number."))
#phonumber.append(phone)
sizeofgroup = int(input('Please enter the size of group: '))
while sizeofgroup < 1 or sizeofgroup > 20:
sizeofgroup = int(input('Please enter a valid group size: '))
groupsizes.append(sizeofgroup)
score = int(input('Please enter the rating of the meal: '))
while score <= 1 or score >= 10:
score = int(input('Please enter the rating of the meal- between 1 and 10: '))
mealscore.append(score)
main()
我收到此错误:
您是否希望继续 Y/N?:y 回溯(最近一次通话):文件\QRSTORAGE\SMCPupilData$\stirlim\Work\S5\Computing\Coursework
Task\Program\code2.py",第 35 行,在 main() 文件中"\QRSTORAGE\SMCPupilData$\stirlim\Work\S5\Computing\Coursework
Task\Program\code2.py", line 16, in main namelist.append(name)NameError: 全局名称namelist"未定义
Do you wish to Continue Y/N?: y Traceback (most recent call last): File "\QRSTORAGE\SMCPupilData$\stirlim\Work\S5\Computing\Coursework
Task\Program\code2.py", line 35, in main() File "\QRSTORAGE\SMCPupilData$\stirlim\Work\S5\Computing\Coursework
Task\Program\code2.py", line 16, in main namelist.append(name) NameError: global name 'namelist' is not defined
不确定错误是什么意思,但它与变量声明有关
Not sure what the error means but ofc its something to do with declaration of variables
推荐答案
因为 Python 使用缩进来确定作用域,所以分配 namelist
必须缩进以与对 name<的分配保持一致/代码>.否则它在函数之外的范围内,并且那里没有名为
namelist
的变量.更有用的错误消息是行需要缩进",但 Python 无法猜测这一点.
Because Python uses indentation for scoping, the assignment namelist
has to be indented to line up with the assignment to name
. Otherwise it is in the scope outside the function, and there is no variable called namelist
there. A more helpful error message would be "line needs to be indented", but there's no way for Python to guess that.
另外,len.str(phone)
失败,因为它试图获取 len
的 str
属性,但它没有得到一.你的意思是len(str(phone))
.
Also, len.str(phone)
fails because it tries to get the str
attribute of len
, and it hasn't got one. You mean len(str(phone))
.
这篇关于未定义全局名称列表.只是模块变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!