This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name = 'Python' | |
if name == 'Python': # 判斷變量是否为'Python' | |
print("Hello World") # 打印Hello World | |
else: | |
print(name) # 條件不成立,打印出變量name |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
score = 85 | |
if score > 90: | |
print('優秀') | |
elif score > 80: | |
print('良好') | |
elif score > 60: | |
print ('及格') | |
else : | |
print ('不及格') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 打印 1—100間的偶數 | |
# 首先創建一個1—100的集合,利用range函數,生成(1-100)的區間,所以最後得+1。 | |
num = range(1, 101) | |
for n in num: | |
if n % 2 == 0: | |
print(n) | |
else: | |
print("以上數字為1-100之内的偶數") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
n = 0 | |
total = 0 | |
while (n < 10): | |
n += 1 # 等同 n = n + 1 | |
total += n | |
print(total) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
n = 0 | |
total = 0 | |
while n < 10: | |
# n += 1 | |
total += n | |
print(total) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
member = 0 | |
amount = 0 | |
total = 0 | |
while amount != -1: | |
member += 1 | |
total += amount | |
amount = int(input("請輸入第 %d 會員消費金額:" % member)) | |
average = total/(member - 1) | |
print("會員總消費金額: %d, 人均消費金額: %5.2f" % (total, average)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
for i in range(11): | |
if i == 7: | |
break | |
print(i) #打印出1~6 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
for i in range(11): | |
if i == 7: | |
continue | |
print(i) # 打印出1~6, 8~10, 中間省略7 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
username = "user" | |
n = 1 | |
while True: | |
a = input("請輸入用戶名:") | |
if n == 3 or a == username: | |
print("輸入正確" if a == username else "輸入錯誤次數已超過3次") | |
break | |
else: | |
print("用戶名有誤,請重新輸入!") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 判斷1-100間的奇數及偶數 | |
for i in range(1, 101): | |
if i % 2 == 0: | |
print("%d是偶數" % i) | |
else: | |
print("%d是奇數" % i) |