用Notepad++打下列的程式碼,
另存為ex19.py. 我附上中文注釋方便好讀.
# -*- coding: UTF-8 -*- # 定義一個函數叫(cheese_and_crackers), 它需要兩個變量(cheese_count和boxes_of_crackers) # 然後印出所要的字跟變量. def cheese_and_crackers (cheese_count, boxes_of_crackers): print (f"You have {cheese_count} cheeses!") print (f"You have {boxes_of_crackers} boxes of crackers!") print ("Man that's enough for a party") print ("Get a blanket. \n") #函數cheese_and_crackers的兩個變量分別為20,30 print ("We can just give the function numbers directly:") cheese_and_crackers (20, 30) #函數cheese_and_crackers的兩個變量分別命名為amount_of_cheese和amount_of_crackers #amount_of_cheese 值為10 #amount_of_crackers 值為50 print("OR, we cab use variables fron our script:") amount_of_cheese = 10 amount_of_crackers = 50 cheese_and_crackers (amount_of_cheese, amount_of_crackers) #函數cheese_and_crackers的兩個變量分別為10 + 20 及 5 + 6 print ("We cab even do math inside too:") cheese_and_crackers (10 + 20, 5 + 6) #函數cheese_and_crackers的兩個變量分別為amount_of_cheese + 100, amount_of_crackers + 1000 #之前我們有定義 #Line 18: amount_of_cheese 值為10 #Line 19: amount_of_crackers 值為50 #故第一個變量 = amount_of_cheese + 100 = 10 + 100 = 110 #第二個變量 = amount_of_crackers + 1000 = 50 + 1000 = 1050 print ("And we cab combine the two, variables and math:") cheese_and_crackers (amount_of_cheese + 100, amount_of_crackers + 1000)
然後用Windows的cmd, 執行python打開它.
C:\Windows\System32>cd C:\Users\Peter\Desktop\Python\LP3THW C:\Users\Peter\Desktop\Python\LP3THW>python .\ex19.py We can just give the function numbers directly: You have 20 cheeses! You have 30 boxes of crackers! Man that's enough for a party Get a blanket. OR, we cab use variables fron our script: You have 10 cheeses! You have 50 boxes of crackers! Man that's enough for a party Get a blanket. We cab even do math inside too: You have 30 cheeses! You have 11 boxes of crackers! Man that's enough for a party Get a blanket. And we cab combine the two, variables and math: You have 110 cheeses! You have 1050 boxes of crackers! Man that's enough for a party Get a blanket. C:\Users\Peter\Desktop\Python\LP3THW>
完成
然後我修改一下,
將Line 18: amount_of_cheese, Lin19: amount_of_crackers
改為該值需為自行手動輸入的整數
# -*- coding: UTF-8 -*- # 定義一個函數叫(cheese_and_crackers), 它需要兩個變量(cheese_count和boxes_of_crackers) # 然後印出所要的字跟變量. def cheese_and_crackers (cheese_count, boxes_of_crackers): print (f"You have {cheese_count} cheeses!") print (f"You have {boxes_of_crackers} boxes of crackers!") print ("Man that's enough for a party") print ("Get a blanket. \n") #函數cheese_and_crackers的兩個變量分別為20,30 print ("We can just give the function numbers directly:") cheese_and_crackers (20, 30) #函數cheese_and_crackers的兩個變量分別命名為amount_of_cheese和amount_of_crackers #amount_of_cheese 值需為自行手動輸入的整數 #amount_of_crackers 值需為自行手動輸入的整數 print("OR, we cab use variables fron our script:") amount_of_cheese = int(input("amount_of_cheese: ")) amount_of_crackers = int(input("amount_of_crackers: ")) cheese_and_crackers (amount_of_cheese, amount_of_crackers) #函數cheese_and_crackers的兩個變量分別為10 + 20 及 5 + 6 print ("We cab even do math inside too:") cheese_and_crackers (10 + 20, 5 + 6) #函數cheese_and_crackers的兩個變量分別為amount_of_cheese + 100, amount_of_crackers + 1000 #之前我們有定義 #Line 18: amount_of_cheese 值為自行手動輸入的整數 #Line 19; amount_of_crackers 值為自行手動輸入的整數 #故第一個變量 = amount_of_cheese + 100 = 輸入值 + 100 #第二個變量 = amount_of_crackers + 1000 = 輸入值 + 1000 print ("And we cab combine the two, variables and math:") cheese_and_crackers (amount_of_cheese + 100, amount_of_crackers + 1000)
結果可以看到:
C:\Users\Peter\Desktop\Python\LP3THW>python .\ex19.py We can just give the function numbers directly: You have 20 cheeses! You have 30 boxes of crackers! Man that's enough for a party Get a blanket. OR, we cab use variables fron our script: amount_of_cheese: 100 amount_of_crackers: 200 You have 100 cheeses! You have 200 boxes of crackers! Man that's enough for a party Get a blanket. We cab even do math inside too: You have 30 cheeses! You have 11 boxes of crackers! Man that's enough for a party Get a blanket. And we cab combine the two, variables and math: You have 200 cheeses! You have 1200 boxes of crackers! Man that's enough for a party Get a blanket. C:\Users\Peter\Desktop\Python\LP3THW>
Line 9: amount_of_cheese輸入值為100, Line 11: 輸出結果為100
Line 23: 輸出結果為100+100 = 200
Line 10: amount_of_crackers輸入值為200, Line 12: 輸出結果為200
Line 24: 輸出結果為200 + 1000 = 1200
最初發表 / 最後更新: 2018.08.21 / 2018.08.21
0 comments:
張貼留言