2017-11-15 1 views
-1

나는 인자로 보내는 두 개의 목록을 비우는 함수를 호출하려고한다. 함수 내에서리스트를 비우고 코드가 파이썬의 원래 함수에서 재사용되면 비어 있는지 확인하는 방법은 무엇입니까? 아래는 큰 시스템에 대한 테스트 실행으로 사용하고있는 코드입니다.파이썬에서 빈 목록을 반환하는 방법

def Input(): 
    nextRound = ['Dad', 'Mom'] 
    maleNames = ['son', 'James', 'Mick', 'Dad'] 

    a = int(input('Enter a number please')) 
    if a == 1: 
     ErrorInput(1, nextRound, maleNames) 
     # I want the below to print two empty strings 
     print(nextRound, maleNames) 

def ErrorInput(error, namelist, round): 


    if error == 1: 
     print('ERROR: You have entered a number above what is impossible to 
     score') 
     namelist = [] 
     round = [] 
    elif error == 2: 
     print('ERROR: You Have entered a score meaning both players win') 
     namelist = [] 
     round = [] 
    elif error == 3: 
     print('ERROR: You have entered a score meaning neither of the two 
     players win') 
     namelist = [] 
     round = [] 
    elif error == 4: 
     print('EEROR: You have entered a negative number, this is 
     impossible') 
     namelist = [] 
     round = [] 

    return(namelist, round) 

Input() 
+0

코드가 사용자 질문과 어떤 관련이 있는지 따라야합니다. 나는 또한 왜이 일을하는지 이해하지 못한다. 분명히 할 수 있니? –

+0

둘 중 어느 하나도 이해할 수 없지만 줄을 'nextRound, maleNames = ErrorInput (1, nextRound, maleNames)'로 바꾸면 업데이트 할 수 있습니다. –

+0

이해가 안됩니다. 'aList = []''aList'라는리스트를 '비우는'것이 아닙니까? – Nae

답변

0

사용 namelist.clear()는 내가 코드를 따르지 않는, 위의 의견에 따라 경찰 목록을

0

여기 코드를 정리하기 위해 자유를 택했습니다.

def start(): # don't use the name input() 
    nextRound = ['Dad', 'Mom'] 
    maleNames = ['son', 'James', 'Mick', 'Dad'] 

    a = int(input('Enter a number please')) 
    if a == 1: 
     nextRound, maleNames = ErrorInput(1, nextRound, maleNames) # if you want nextRound, maleNames to be [] you have to assign it to them 
     print(nextRound, maleNames) 

def ErrorInput(error, namelist, round): 
    error_codes = {1: 'ERROR: You have entered a number above what is impossible to score', 2: 'ERROR: You Have entered a score meaning both players win',3: 'ERROR: You have entered a score meaning neither of the two players win', 4: 'ERROR: You have entered a negative number, this is impossible'} # too many ifs makes one wonder if a dictionary is the way to go 
    print(error_codes.get(error, 'Unknown Error')) 
    return([], []) # return the empty lists. 

start() 

없음 당신은 확실히 단지 [], []를 얻기 위해 같은 길이로 이동하지만 배를 수레 어떤 이유.


내가 변경 한 이유에 대해 더 잘 알고 싶다면 코드의 메모를 확인하십시오.

0

을 취소합니다. 그러나 반복적으로 목록을 비우려면 다음을 시도하십시오.

while len(maleNames) !=0: 
    popped = maleNames.pop() 
    print('Popped value: ', popped, '\tNew list: ', maleNames) 
+0

@NickA 고맙습니다. 네. –

관련 문제