1. 程式人生 > >《“笨辦法”學python3》Ex 13

《“笨辦法”學python3》Ex 13

知識點:

寫一個接受引數的指令碼. 

按照程式輸入,出現報錯:not enough values to unpack (expected 4, got 1)

原因在於沒有新增命令列引數

由於設定的引數是4個,因此包括檔名應該有4個引數,否則報錯

程式:

from sys import argv
# read the WYSS section for how to run this
script,first,second,third = argv

print("The script is called:", script)
print("Your first variable is:", first)
print("Your second variable is:", third)

輸出:

PS C:\Users\xue weiruan\github> python ex13.py 1 2 3
The script is called: ex13.py
Your first variable is: 1
Your second variable is: 3
PS C:\Users\xue weiruan\github> python ex13.py my name xp
The script is called: ex13.py
Your first variable is: my
Your second variable is: xp
PS C:\Users\xue weiruan\github> python ex13.py my name
Traceback (most recent call last):
  File "ex13.py", line 3, in <module>
    script,first,second,third = argv
ValueError: not enough values to unpack (expected 4, got 3)
PS C:\Users\xue weiruan\github> python ex13.py my name xp xpp
Traceback (most recent call last):
  File "ex13.py", line 3, in <module>
    script,first,second,third = argv
ValueError: too many values to unpack (expected 4)
PS C:\Users\xue weiruan\github>