0

私は一連のチュートリアルを見てpython3を学んで
います。オプションの関数の引数()に関するこれらのビデオの1つで*args、インストラクターはforループを使用して関数(タプル)に渡されるオプションのパラメーターを出力します。

インストラクターのスクリプトを実行しようとすると、エラーが発生します。


インストラクターのスクリプト:

def test(a,b,c,*args):  
    print (a,b,c)  
for n in args:  
    print(n, end=' ')  

test('aa','bb','cc',1,2,3,4)  

出力:

C:\Python33\python.exe C:/untitled/0506.py  
Traceback (most recent call last):  
  File "C:/untitled/0506.py", line 4, in <module>  
    for n in args: print(n, end=' ')  
NameError: name 'args' is not defined  

Process finished with exit code 1  

def test(a,b,c,*args):  
    print (a,b,c)  
    print (args)  

test('aa','bb','cc',1,2,3,4)  

出力:

aa bb cc  
(1, 2, 3, 4)  
Process finished with exit code 0  

エラーの原因は何ですか?
PS:私はPython3.3.0を使用しています。

4

1 に答える 1

2

インデントが間違っています:

def test(a,b,c,*args):  
    print (a,b,c)  
    for n in args:  
        print(n, end=' ')  

test('aa','bb','cc',1,2,3,4)  

Pythonではインデントが重要です。あなたのバージョンは関数のfor n in args:ループを宣言したので、すぐに実行されました。はへのローカル変数であるため、関数の外部では定義されておらず、を取得します。test()argstest()NameError

于 2013-03-25T18:14:02.273 に答える