Skip to content

Instantly share code, notes, and snippets.

@Celia-code
Celia-code / if1.py
Last active March 30, 2020 03:57
if語句
name = 'Python'
if name == 'Python': # 判斷變量是否为'Python'
print("Hello World") # 打印Hello World
else:
print(name) # 條件不成立,打印出變量name
@Celia-code
Celia-code / if2.py
Created March 30, 2020 03:58
if條件語句
score = 85
if score > 90:
print('優秀')
elif score > 80:
print('良好')
elif score > 60:
print ('及格')
else :
print ('不及格')
@Celia-code
Celia-code / for.py
Created April 3, 2020 05:31
for 條件語句
# 打印 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之内的偶數")
@Celia-code
Celia-code / while1.py
Last active April 6, 2020 09:57
while循環
n = 0
total = 0
while (n < 10):
n += 1 # 等同 n = n + 1   
total += n
print(total)
@Celia-code
Celia-code / while2.py
Created April 6, 2020 09:23
while循環
n = 0
total = 0
while n < 10:
# n += 1
total += n
print(total)
@Celia-code
Celia-code / while3.py
Created April 6, 2020 09:58
while循環
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))
@Celia-code
Celia-code / b&c1.py
Created April 9, 2020 10:02
break&continue
for i in range(11):
if i == 7:
break
print(i) #打印出1~6
@Celia-code
Celia-code / b&c2.py
Last active April 9, 2020 10:19
break&continue
for i in range(11):
if i == 7:
continue
print(i) # 打印出1~6, 8~10, 中間省略7
@Celia-code
Celia-code / b&c3.py
Last active April 9, 2020 11:10
break&continue
username = "user"
n = 1
while True:
a = input("請輸入用戶名:")
if n == 3 or a == username:
print("輸入正確" if a == username else "輸入錯誤次數已超過3次")
break
else:
print("用戶名有誤,請重新輸入!")
@Celia-code
Celia-code / if.py
Created April 13, 2020 12:10
嵌套
# 判斷1-100間的奇數及偶數
for i in range(1, 101):
if i % 2 == 0:
print("%d是偶數" % i)
else:
print("%d是奇數" % i)