1、if語句的練習
cars=['aodi','bmw','falali','lanbojini'] for car in cars: if car == 'bmw': print(car.upper()) else: print(car.lower())
2、外星人顏色的練習
alien_color=['green','yellow','red'] if alien_color =='green': count =5 if alien_color=='yellow': count=10 if alien_color=='red': count=15 print(' the player get %d point!'%count)
3、游樂場收費的練習
age=int(input()) if age<4: price =0 elif 4<=age<18: price =5 else: price =10 print('shoufei: %d元'%price)
4、是否在列表中
a1=[1,2,3,4,5]
a2=9
if a2 in a1:
print('in list')
else:
print('not in list !')
5、序數
#序數表示位置,如1st 和2nd。大多數序數都以th結尾,只有1、2和3例外。 #在一個列表中存儲數字1~9。 #遍歷這個列表。 #在循環中使用一個if-elif-else 結構,以打印每個數字對應的序數。 # 輸出內容應為1st、2nd、3rd、4th、5th、6th、7th、8th 和9th,但每個序數都獨占一 行。 a=[] for i in range(1,10): a.append(i) for b in a: if b ==1: print(str(b)+'st') elif b==2: print(str(b)+'nd') elif b==3: print(str(b) + 'rd') else: print(str(b) + 'th')