Stage 2: Work Session5

Stage 2: Work Session5

Practice With Lists

學習使用隨機變數

  • 先 import random

  • 語法:using the random.randint(start, end) function

  • 像 random.randint(0,10)

  • If we want to concatenate a string and a number, we need to convert the integer into a string using the str() function

  • e.g. print "Random number generated: " + str(random.randint(0,10))

Product List練習:

每個數字互相乘

找出最大的數字

Split

Spit 的相反,join

找出二個字串中相同的字

困難度高的案例 -play game

  • 先把ml_string split 成一個個字串

  • 設一個 for loop , go through each word in 剛轉成字串的 ml_string

  • 找出 word 裡和 parts_of_speech 相同的值,assign 到 replacement 的變數

  • 細節: ml_string 裡有一個值 ADJECTIVE! (內有驚嘆號)遇上parts_of_speech 中沒有驚嘆號的 ADJECTIVE,replacement 會顯示沒有驚嘆號的

  • 如果對應上就顯示 None

  • Check 如果 replacement 不是 None,就把重複的字(replacement)換成corgi

  • relaced 這個空 list 加進 word (已經換好的字串堆)

把 replaced 字串堆 join 成一串

  • 最後用 raw_input 方程式去換 corgi,創造 user_input

終端機的使用- Terminal

  • Terminal lets you interact with your computer at a lower level than is normally possible by typing commands into what is called a "shell."

  • Terminal is installed by default in a folder called "Utilities" within your "Applications" folder.

  • cd: Stands for "change directory."

  • ls: Stands for "list." The ls command lists all files and folders in the current directory.

IDLE- Python Shell

raw_input function

什麼是.pyc file

  • A .pyc file is a Python file that has been translated into "byte code".

  • 寫程式 Take a break

需求:二十分鐘就開 Youtube 聽歌休息一下

results matching ""

    No results matching ""