2014-03-07 2 views
0

입니다. 그렇다면이 코드를 사용하여 몇 가지 테스트를 실행하여 주된 문제점을 확인하십시오. 나는 비활성, 그래서 그것을 에드 '# 내가 그래서 전화 번호 입력 유효성 검사에 문제가 발견 (당신은 당신에게 스티커를 lgive 줘야 그 문제를 해결 할 수있는 경우 을 어쨌든 :..전역 이름 목록이 정의되지 않았습니다. 모듈 변수는

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() 

그리고 난

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

어떤 오류 수단하지만 파이썬은 범위 지정에 대한 들여 쓰기를 사용하기 때문에 변수

답변

2

의 선언을 함께 할 수있는 그 무언가 OFC 확실하지, 할당 namelist가 함께 줄을 들여 쓰기되어야한다 :이 오류 할당 name. 그렇지 않으면 함수 밖의 범위에 있으며 namelist이라는 변수가 없습니다. 더 유용한 오류 메시지는 "줄을 들여 써야합니다."이지만 파이썬이이를 추측 할 수있는 방법은 없습니다.

또한 str 속성을 얻으려고 시도하기 때문에 len.str(phone)이 실패하고 len이 없으므로 실패했습니다. len(str(phone))을 의미합니다.

+0

감사합니다. 이름 목록 섹션이 작동합니다 ^^ 불행히도 전화 내용은 부담스럽지 않지만 ^^ 노력하고 있습니다 ^^ – Minuit

+0

실제로는 식별자에 관한 것이 아니라 기능 정의에 관한 것입니다. 파이썬은 범위 지정을 위해 식별자를 사용하지 않습니다. if 블록 안에 변수를 정의하려고합니다 : if True : my_var = 10; print (my_var)'가 작동합니다;) – ProfHase85

관련 문제