2017-11-15 11 views
-1

私は引数として送信する2つのリストを空にする関数を呼び出そうとしています。 関数内のリストを空にして、Pythonの元の関数でコードを再利用するようになったら、それらが空であることを確認するにはどうすればよいですか?以下は、私が大規模なシステムでテストを実行する際に使用しているコードです。Pythonで空リストを返す方法

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

どちらか分かりませんが、行7を '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ありがとう、はい。 –

関連する問題