### Lab7-1 ### a = int(input("Enter an interger: ")) if a < 1: a += 1 b = a + 3 print(b) ### Lab7-2 (ex7-1) ### num = int(input("Enter a number: ")) if num > 0: print("num is positive") print("finished comparing num to 0") ### Lab7-3 (觀念驗證 7.2) ### num = int(input("Enter a number: ")) if num < 10: print("num is less than 10") print("Finished") ### Lab7-4 (觀念驗證 7.3) ### # 第 1 題 word = input("Tell me a word: ") print(word) if " " in word: print("You did not follow directions.") # 第 2 題 num1 = int(input("One number: ")) num2 = int(input("Another number: ")) print(num1 + num2) if num1 + num2 > 10000: print("Wow, huge sum!") ### Lab7-5 (ex7-2) ### num_a = int(input("Pick a number: ")) if num_a > 0: print("Your number is positive") if num_a < 0: print("Your number is negative") if num_a == 0: print("Your number is zero") print("Finished!") ### Lab7-6 (ex7-3) ### # 巢狀條件式 num_a = int(input("Number? ")) num_b = int(input("Number? ")) if num_a < 0: print("num_a is negative") if num_b < 0: print("num_b is negative") print("Finished") # 非巢狀條件式 num_a = int(input("Number? ")) num_b = int(input("Number? ")) if num_a < 0: print("num_a is negative") if num_b < 0: print("num_b is negative") print("Finished") ### Lab7-7 (ex7-4) ### price = float(input("How much does a chocolate bar cost? ")) hungry = input("Are you hungry (yes or no)? ") bars = 0 if hungry == "yes": if price < 10: print("Buy all chocolate bars.") bars = 100 if 10 <= price <= 50: print("Buy 10 chocolate bars.") bars = 10 if price > 50: print("Buy only one chocolate bar.") bars = 1 if hungry == "no": print("Stick to the shopping list.") if bars > 10: print("Cashier says: someone's hungry!") ### Lab7-8 (ex7-5) ### # 多條件運算式 num_a = int(input("Number? ")) num_b = int(input("Number? ")) if num_a < 0 and num_b < 0: print("both negative") # 巢狀條件式 num_a = int(input("Number? ")) num_b = int(input("Number? ")) if num_a < 0: if num_b < 0: print("both negative") ### Lab7-9 (ex7-6) ### Num = int(input("please pick a number: ")) if Num % 2 == 0: print("Even Number") else: print("Odd Number") ### Lab7-10 (觀念驗證 7.5) ### Num = int(input("please pick a number: ")) if Num > 0: print("Positive Number") else: print("Negative Number") ### Lab7-11 (ex7-7) ### num = int(input("Enter a number: ")) if num > 0: print("num is positive") elif num < 0: print("num is negative") else: print("num is zero") ### Lab7-12 (觀念驗證 7.5) ### # if-elif-else 敘述 num = int(input("Enter a number: ")) if num < 6: print("num is less than 6") elif num < 10: print("num is less than 10") elif num > 3: print("num is greater than 3") else: print("No relation found.") print("Finished.") # if 敘述 num = int(input("Enter a number: ")) if num < 6: print("num is less than 6") if num < 10: print("num is less than 10") if num > 3: print("num is greater than 3") print("Finished.") ### Lab7-13 (ex7-8) ### num_a = int(input("pick a number: ")) num_b = int(input("pick another number: ")) lucky_num = 7 if num_a == num_b: print("You enter the same number") else: if num_a > 0 and num_b > 0: print("both numbers are positive") elif num_a < 0 and num_b < 0: print("both numbers are negative") else: print("numbers have opposite sign") if num_a == lucky_num or num_b == lucky_num: print("you also guessed my lucky number!") else: print("I have a secret number in mind...") ### Lab7-14 (ex7-9) ### num_a = int(input("pick a number: ")) num_b = int(input("pick another number: ")) lucky_num = 7 if num_a == num_b: print("You enter the same number") elif num_a > 0 and num_b > 0: print("both numbers are positive") elif num_a < 0 and num_b < 0: print("both numbers are negative") else: print("numbers have opposite sign") if num_a == lucky_num or num_b == lucky_num: print("you also guessed my lucky number!") else: print("I have a secret number in mind...") ### Lab7-15 (ex7-10) ### greeting = input("Say hi in English or Spanish! ") greet_en = ("hi", "Hi", "hello", "Hello") greet_sp = ("hola", "Hola") if greeting not in greet_en and greeting not in greet_sp: print("I don't understand your greeting.") elif greeting in greet_en: num = int(input("Enter 1 or 2: ")) print("You speak English!") if num == 1: print("one") elif num == 2: print("two") elif greeting in greet_sp: num = int(input("Enter 1 or 2: ")) print("You speak Spanish!") if num == 1: print("uno") elif num == 2: print("dos") ### Lab7-16 (ex7-11) ### print("You are on a deserted island in a 2D world.") print("Try to survive until rescue arrives!") print("Available commands are in CAPITAL letters.") print("Any other command exits the program") print("First LOOK around...") ### Lab7-17 (ex7-12) ### do = input(":: ") if do == "LOOK": print("You are stuck in a sand ditch.") print("Crawl out LEFT or RIGHT.") do = input(":: ") if do == "LEFT": print("You make it out and see a ship!") print("You survived!") elif do == "RIGHT": print("No can do. That side is very slippery.") print("You fall very far into some weird cavern.") print("You do not survive :(") else: print("You can only do actions shown in capital letters.") print("Try again!") ### Lab7-18 (ex7-13) ### print("You are on a deserted island in a 2D world.") print("Try to survive until rescue arrives!") print("Available commands are in CAPITAL letters.") print("Any other command exits the program") print("First LOOK around...") do = input(":: ") if do == "LOOK": print("You are stuck in a sand ditch.") print("Crawl out LEFT or RIGHT.") do = input(":: ") if do == "LEFT": print("You see a STARFISH and a CRAB on the sand.") print("And you're hungry! Which do you eat?") do = input(":: ") if do == "STARFISH": print("Oh no! You immediately don't feel well.") print("You do not survive :(") elif do == "CRAB": print("Raw crab should be fine, right? YES or NO.") do = input(":: ") if do == "YES": print("Ok, You eat it raw. Fingers crossed.") print("Food in your belly helps you see a TREE.") do = input(":: ") if do == "TREE": print("It's a coconut tree! And you're thirsty!") print("Do you drink the coconut water? YES or NO.") do = input(":: ") if do == "YES": print("Oh boy. Coconut water and rawcrab don't mix.") print("You do not survive :(") elif do == "NO": print("Good choice.") print("Look! It's a rescue plane! You made it! \o/") elif do == "NO": print("Well, there's nothing else left to eat.") print("You do not survive :(") elif do == "RIGHT": print("No can do. That side is very slippery.") print("You fall very far into some weird cavern.") print("You do not survive :(") else: print("You can only do actions shown in capital letters.") print("Try again!")