1. 程式人生 > >python3--break與continue

python3--break與continue

#break的用法
import random
result = random.randint(0,100)    
for i in range(3):
    #print('please input your number,you have 3 times:')
    key = int(input('請輸入你猜的數字:'))
    
    if key == result:
        print ('bingo!')
        break
    elif key > result:
        print ('you have big number!')

    else:
        print('you have small number!')

print('aho!you have used your chance')

#continue
#使用continue判斷,課堂測試成績小於60分,不計入總成績

results = []
with open ('scores.txt',mode='r',encoding='gbk') as f1:
    lines = f1.readlines()
    f1.close()
    #print (lines)
    sum = 0
    for line in lines:
        data = line.split()
##        print (data)
        for i in data[1:]:
            if int(i) < 60:
                continue
            else:
                sum += int(i)
        result = '%s:%d' %(data[0],sum)
##        print (result)
        results.append(result)
print(results)
with open('output.txt',mode='w+',encoding='gbk') as f2:
    f2.writelines(results)
    f2.close()