### Lab8-1 ### print("abc") print("abc") print("abc") ### Lab8-2 (ex8-1) ### s = input("請輸入顯示內容: ") n = int(input("請輸入顯示次數: ")) while n > 0: print(s) n = n - 1 ### Lab8-3 (觀念驗證 8.1) ### s = input("請輸入顯示內容: ") n = int(input("請輸入顯示次數: ")) while n > 0: print(s) n = n - 1 ### Lab8-4 (ex8-2) ### lucky_num = 77 guess = int(input("Guess a Number(0-99): ")) tries = 1 while guess != lucky_num: print("You tried to guess", tries, "times") guess = int(input("Guess again: ")) tries += 1 print("You got it!") ### Lab8-4 (觀念驗證 8.2) ### lucky_num = 77 guess = int(input("Guess a Number(0-99): ")) tries = 1 while guess != lucky_num: tries += 1 print("You tried to guess", tries, "times") guess = int(input("Guess again: ")) print("You got it!") ### Lab8-5 (ex8-3) ### secret = "code" max_tries = 100 guess = input("Guess a word: ") tries = 1 while guess != secret: print("You tried to guess", tries, "times") if tries == max_tries: print("You ran out of tries.") break guess = input("Guess again: ") tries += 1 if tries <= max_tries and guess == secret: print("You got it!") ### Lab8-6 (ex8-4) ### i = 1 while(i < 10): if i == 5: i += 1 continue print(i, end = ' ') i += 1 print("End") ### Lab8-7 (ex8-5) ### s = "" while s != "喵喵": if s != "": print("不對喔!") s = input("請輸入通關密語:") if s == "out": break else: print("恭喜你過關了") print("再見!") ### Lab8-8 (ex8-6) ### while True: print("when will it end?!") ### Lab8-9 (ex8-7) ### # 使用 for 迴圈 for i in range(10): print("Hello Python") # 不使用迴圈 print("Hello Python") print("Hello Python") print("Hello Python") print("Hello Python") print("Hello Python") print("Hello Python") print("Hello Python") print("Hello Python") print("Hello Python") print("Hello Python") ### Lab8-10 (ex8-8) ### for v in range(2): print("var v is", v) ### Lab8-11 (觀念驗證 8.3) ### # range(1) for i in range(1): print(i) # range(5) for i in range(5): print(i) # range(100) for i in range(100): print(i) ### Lab8-12 (ex8-9) ### for i in range(10, 1, -1): print(i, end = " ") print() for i in range(0, 9, 2): print(i, end = " ") ### Lab8-13 (觀念驗證 8.4) ### # range(0, 9) for i in range(0, 9): print(i) # range(3, 8) for i in range(3, 8): print(i) # range(-2, 2) for i in range(-2, 2): print(i) # range(5, -5, -3) for i in range(5, -5, -3): print(i) # range(4, 1, 2) for i in range(4, 1, 2): print(i) ### Lab8-14 (ex8-10) ### for i in range(1, 10): for j in range(1, 10): print(j, "x", i, "=", j * i, end = ' ') print() ### Lab8-15 (觀念驗證 8.5) ### mins=int(input("please input Minutes to countdown: ")) for i in range(mins-1, -1, -1): for j in range(59, -1, -1): print(i, ":", j) ### Lab8-16 (ex8-11) ### for i in range(1, 20): if i == 5: continue print(i, end = ' ') if i == 10: break print("END") ### Lab8-17 (ex8-12) ### num = int(input("Please specify the range(0~N): ")) for i in range(2, num + 1): for j in range(2, i): if(i % j == 0): break else: print(i) ### Lab8-18 ### # tuple 容器 >>> tuple = (1, '2', 3) >>> tuple[0] # 串列容器 (list) >>> list = [1, '2', 3] >>> list[2] # 集合容器 (set) >>> list = [1, '2', 3] >>> list[1] # 字典容器 (directory) >>> dick = {'A':1, 'B':'2', 'C':3} >>> dick['B'] # Numpy >>> import numpy as np # 匯入 numpy >>> a = np.array([10, 2, 45, 32, 24]) # 建立五個元素 >>> len(a) # 計算元素個數 >>> a[2:4] # 取出第 2、3 個元素 >>> a[:4] # 取出第 1 ~ 3 個元素 ### Lab8-19 (ex8-13) ### for ch in "Python is fun!": print("the character is", ch) ### Lab8-20 (觀念驗證 8.6) ### vowels = "aeiou" words = input("Tell me something: ") for letter in words: if letter in vowels: print("vowel") ### Lab8-21 (ex8-14) ### my_string = "Python is fun!" len_s = len(my_string) for i in range(len_s): print("the character is", my_string[i]) ### Lab8-22 (ex8-15) ### winners = ("Peter", "Mark", "Joy") print("恭喜以下得獎人:") for name in winners: print(name) ### Lab8-23 (ex8-16) ### prizes = ("頭獎", "二獎", "三獎") winners = ("Peter", "Mark", "Joy") print("恭喜以下得獎人:") for i in range(3): print(prizes[i]+ ":" + winners[i]) ### Lab8-24 (ex8-17) ### winners = (("頭獎", "Peter"), ("二獎", "Mark"), ("三獎", "Joy")) print("恭喜以下得獎人:") for e in winners: print(e[0] + ":" + e[1]) ### Lab8-25 (觀念驗證 8.7) ### name=(("張", "天才"), ("王", "子帥"), ("陳", "美美")) print("全名是: ") for e in name: print(e[0] + e[1]) ### Lab8-26 ### for (a, b) in ((1, 2), (3, 4)): print(a, b) ### Lab8-27 (ex8-18) ### winners = (("頭獎", "Peter"), ("二獎", "Mark"), ("三獎", "Joy")) print("恭喜以下獎人:") for prize, winner in winners: print(prize + ":" + winner) ### Lab8-28 ### >>> a, b = 1, 2 >>> a, b = b, a >>> x, (y, z) = 1, (2, 3) ### Lab8-29 (觀念驗證 8.8) ### student = ((22, ("張", "天才")), (23, ("王", "子帥")), (24, ("陳", "美美"))) for no, name in student: print(str(no) + " 號 - " + name[0] + name[1]) ### Lab8-30 (ex8-19) ### # for 迴圈 for x in range(3): print("var x is", x) # while 迴圈 x = 0 while x < 3: print("var x is", x) x += 1 ### Lab8-31 (觀念驗證 8.9) ### password = "robot fort flower graph" space_count = 0 for ch in password: if ch == " ": space_count += 1 print(space_count) ### Lab8-32 ### word = """ art hue ink oil crosshatching """ ### Lab8-33 ### word = """art hue ink """ ### Lab8-34 (ex8-20) ### word = """art hue ink oil crosshatching """ tiles = "hijklmnop" # 可以隨意變更 title 變數內容,看看可以組出哪些單字 all_valid_words = () start = 0 end = 0 found_words = () ### Lab8-35 (ex8-21) ### for char in words: if char == "\n": all_valid_words = all_valid_words + (words[start:end], ) start = end + 1 end = end + 1 else: end = end + 1 ### Lab8-36 (ex8-22) ### for word in all_valid_words: tiles_left = tiles for letter in word: if letter not in tiles_left: break else: index = tiles_left.find(letter) tiles_left = tiles_left[:index]+tiles_left[index + 1:] if len(word) == len(tiles) - len(tiles_left): found_words = found_words + (word, ) print(found_words) ### Lab8-37 (ex8-23_Scrabble Game.py) ### words = """art hue ink oil pen wax clay draw film form kiln line tone tube wood batik brush carve chalk color craft easel erase frame gesso glass glaze image latex liner media mixed model mural paint paper photo print quill quilt ruler scale shade stone style tools video wheel artist bridge canvas chisel crayon create depict design enamel eraser fresco hammer marble marker medium mobile mosaic museum pastel pencil poster pounce roller sculpt sketch vellum visual acrylic artwork cartoon carving casting collage compass drawing engrave etching exhibit gallery gilding gouache painter palette pigment portray pottery primary realism solvent stained stencil tempera textile tsquare varnish woodcut abstract airbrush artistic blending ceramics charcoal contrast critique decorate graffiti graphite hatching maquette marbling painting portrait printing quilting sculptor seascape template animation cloisonne decoupage encaustic engraving landscape porcelain portfolio sculpture secondary stippling undertone assemblage brightness creativity decorative exhibition illustrate lithograph paintbrush photograph proportion sketchbook turpentine watercolor waterscape calligraphy composition masterpiece perspective architecture glassblowing illustration installation stonecutting crosshatching """ tiles = "hijklmnop" #可以隨意變更 title 變數內容,看看可以組出哪些單字 all_valid_words = () start = 0 end = 0 found_words = () for char in words: if char == "\n": all_valid_words = all_valid_words + (words[start:end],) start = end + 1 end = end +1 else: end = end + 1 for word in all_valid_words: tiles_left = tiles for letter in word: if letter not in tiles_left: break else: index = tiles_left.find(letter) tiles_left = tiles_left[:index]+tiles_left[index+1:] if len(word) == len(tiles)-len(tiles_left): found_words = found_words + (word,) print(found_words)