日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

learnpython3thehardway视频_LearnPython3theHardWay__Excercise 13 Parameters, Unpacking, Variables

發布時間:2023/12/4 python 38 豆豆
生活随笔 收集整理的這篇文章主要介紹了 learnpython3thehardway视频_LearnPython3theHardWay__Excercise 13 Parameters, Unpacking, Variables 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

建議ex11-14連起來學習,主要講input

參數,拆包,變量

我們繼續深入學習input()。在這一節,我們能用另一種 input 方法,傳遞變量給我們的腳本 ex13. py

創建ex13.py文件,然后敲鍵盤打以下內容并保存

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:", second)

print("Your third varialbe is:", third)

第一行,import,我們稱為導入,從sys模塊導入argv。argv 是一個參數變量,你可以在Idle里輸入type(argv),可以看到,它的類型是list,列表。第三行,我們從argv取出4個變量,分別命名為script, first, second, third。(我們稱之為unpack——拆包,想象argv是一個箱子,我們從里面取出4個參數, 不知道這么說形象不?)

What you should see

注意看,你要怎么運行 ex13. py。在命令行提示符cmd里,輸入第一行和結果顯示,注意路徑,建議用pycharm,對著ex13.py右鍵,選open in terminal

C:\PycharmProjects\learnpythonthehardway>python ex13.py first 2nd 3rd

The script is called: ex13.py

Your first variable is: first

Your second variable is: 2nd

Your third varialbe is: 3rd

換不一樣的參數,再看看

C:\PycharmProjects\learnpythonthehardway>python ex13.py stuff things that

The script is called: ex13.py

Your first variable is: stuff

Your second variable is: things

Your third varialbe is: that

C:\PycharmProjects\learnpythonthehardway>python ex13.py apple orange grapefruit

The script is called: ex13.py

Your first variable is: apple

Your second variable is: orange

Your third varialbe is: grapefruit

實際上,我們只需要把first 2nd 3rd覆蓋成任何你想輸入的三樣東西。

如果你沒正確的輸入,你會得到錯誤的信息

$ python ex13.py first 2nd

Traceback (most recent call last):

File "ex13.py", line 3, in script, first, second, third = argv

ValueError: not enough values to unpack (expected 4, got 3)

這個錯誤是告訴我們,沒有提供足夠的參數給argv,程序里只給了first 和 2nd。

Study drills

1、試著給少于3個參數,看看錯誤信息

2、寫一個比例子參數少的腳本和一個比例子參數多的腳本。

3、結合input和argv來從使用者那里獲得更多的輸入。別想太復雜。

4、記住模塊。

總結

以上是生活随笔為你收集整理的learnpython3thehardway视频_LearnPython3theHardWay__Excercise 13 Parameters, Unpacking, Variables的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。